Objective-C Protocol Forward Declarations

前端 未结 4 1976
心在旅途
心在旅途 2020-12-03 00:31

ObjectProperties.h

@protocol ObjectProperties 

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *         


        
4条回答
  •  渐次进展
    2020-12-03 00:52

    Yes you are correct that all of the files will need to be recompiled, but this is necessary. Files that import the class header need to know the methods associated with the protocol it implements. If you tuck that definition away in the .m file then it is only visible to one file (since .m files never get imported). It's not the same as forward declaring classes. If you forward declare a protocol, you must declare it somewhere that is visible in the same scope as the forward declaration. I can't think of any example where this doesn't occur in the same file.

    In ARC this is an error, because ARC needs to know about the memory management of the declared methods (do they return +1 instances, internal pointers, etc?)

提交回复
热议问题