Objective C Delegate declaration

前端 未结 3 824
日久生厌
日久生厌 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 
    // ...
    @end
    

    The 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 
    @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 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. ) - for a more complete overview of Protocols, check out the Apple Docs on protocols.

提交回复
热议问题