Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Delegate declaration

Can someone tell me the difference between

@property (nonatomic, weak) id delegate;

@property (nonatomic, weak) id  <protocol_name> delegate;

@property (nonatomic, weak) UIViewController * <protocol_name> delegate;
like image 914
Preetham Baliga Avatar asked Apr 16 '14 10:04

Preetham Baliga


People also ask

How do you declare a protocol in Objective-C?

Any class that declares itself to conform to this protocol must implement the methods and properties dictated in the protocol. In Objective-C, protocols are declared with the “@protocol” keyword.

What is the difference between delegate and datasource?

A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data. A data source is an outlet held by NSView and UIView objects such as table views and outline views that require a source from which to populate their rows of visible data.

What is Delegation in C?

C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.

What is delegate protocol in iOS?

Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol.


2 Answers

@property (nonatomic, weak) id delegate;

This specifies that objects of the current class have a delegate that can be of any type. The weak specifier is common for delegate objects as it means the object with the delegate does not increment the delegate's reference count (in ARC-speak "keep a strong reference of it"). A weak delegate reference is standard practice.

@property (nonatomic, weak) id < protocol_name > delegate;

This specifics that objects of the current class have a delegate that can be of any type (id) but must conform to the protocol_name protocol. This is particularly useful as the class containing the delegate knows that there are specific messages that it can send to its delegate and "know" that the delegate will respond to them.

@property (nonatomic, weak) UIViewController * < protocol_name > delegate;

This is the same as the second example except that the delegate must be an object of class UIViewController. In practice, delegate objects are usually of type id, though this is not a requirement - it just offers greater freedom to the programmer.


EDIT: Protocols

Let's say you declare a class as follows:

@interface MyObject : NSObject <MyDelegateProtocol>
// ...
@end

The <MyDelegateProtocol> in this declaration means that MyObject implements the methods defined in the MyDelegateProtocol protocol (i.e. 'conforms to the protocol').

The protocol definition (previous to the class definition, obviously) may look like this:

@protocol MyDelegateProtocol <NSObject>
@required
- (void)method1;
- (void)method2;
@optional
- (void)method3;
@end

This means that any object 'conforming' to the MyDelegateProtocol protocol must implement methods called -(void)method1 and -(void)method2. And, optionally, may include an implementation for the message -(void)method3.

This is extremely useful information for delegate objects (the protocol name could be anything by the way, I just include the word 'delegate' to make it obvious that it is used as a delegate protocol).

If another class now defines:

@property (nonatomic, weak) id<MyDelegateProtocol> delegate;

the class knows that it can rely on implementations of -method1 and -method2 to be implemented by its delegate, and -method3 may be implemented as well (which it can check with code such as the following:)

if ([self.delegate respondsToSelector:@selector(method3)]) {
    [self.delegate method3];
}
else {
    // Delegate doesn't implement -method3.
}

The check is unnecessary for -method1 and -method2 as these methods are @required by the protocol definition, and it can call them whenever it wants.

A class can also use more than one protocol at a time (e.g. <Proto1, Proto2, UITableViewDelegate>) - for a more complete overview of Protocols, check out the Apple Docs on protocols.

like image 118
Ephemera Avatar answered Sep 30 '22 03:09

Ephemera


@property (nonatomic, weak) id delegate;

A property with no specific type or protocol implementation. When calling methods on delegate, anything goes - the compiler will trust you if it can see that a method exists somewhere and the runtime will check if you were lying.

@property (nonatomic, weak) id < protocol_name > delegate;

A property with no specific type, but which implements a specified protocol. You can only call methods from that protocol (unless you do some casting). Any instance that is set to the property must conform to the protocol (or again, you need some casting).

@property (nonatomic, weak) UIViewController * < protocol_name > delegate;

A property with a specific type (UIViewController) and which implements a specified protocol. You can only call methods from that protocol and from the UIViewController class (unless you do some casting). Any instance that is set to the property must conform to the protocol and be a subclass of UIViewController (or again, you need some casting).

like image 45
Wain Avatar answered Sep 30 '22 04:09

Wain