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;
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;
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.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).
A property with a specific type (
UIViewController
) and which implements a specified protocol. You can only call methods from that protocol and from theUIViewController
class (unless you do some casting). Any instance that is set to the property must conform to the protocol and be a subclass ofUIViewController
(or again, you need some casting).In the first example:
you create a property of type id which is 'any' type in objective c with name - delegate.
The second example:
you create a property of type id which needs to conform to protocol_name protocol with name - delegate.
The last example:
you create a property of type UIViewController (pointer to UIViewController) which needs to conform to protocol_name protocol with name - 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"). Aweak
delegate reference is standard practice.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.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 typeid
, 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:
The
<MyDelegateProtocol>
in this declaration means thatMyObject
implements the methods defined in theMyDelegateProtocol
protocol (i.e. 'conforms to the protocol').The protocol definition (previous to the class definition, obviously) may look like this:
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:
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:)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.