Objective C Delegate declaration

前端 未结 3 829
日久生厌
日久生厌 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: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).

提交回复
热议问题