Objective C Delegate declaration

前端 未结 3 828
日久生厌
日久生厌 2021-02-07 08:58

Can someone tell me the difference between

@property (nonatomic, weak) id delegate;

@property (nonatomic, weak) id   delegate;

@property          


        
相关标签:
3条回答
  • 2021-02-07 09:26

    @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.

    0 讨论(0)
  • 2021-02-07 09:28

    In the first example:

    @property (nonatomic, weak) id delegate;
    

    you create a property of type id which is 'any' type in objective c with name - delegate.

    The second example:

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

    you create a property of type id which needs to conform to protocol_name protocol with name - delegate.

    The last example:

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

    you create a property of type UIViewController (pointer to UIViewController) which needs to conform to protocol_name protocol with name - delegate.

    0 讨论(0)
  • 2021-02-07 09:34

    @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).

    0 讨论(0)
提交回复
热议问题