Hiding properties from public framework headers, but leaving available internally

前端 未结 1 1707
悲哀的现实
悲哀的现实 2020-12-18 13:25

I need to have property in class that is excluded from public framework headers, but it is available for use internally in other framework classes. What I did right now is:<

相关标签:
1条回答
  • 2020-12-18 13:28

    I think you could do with the class extension only, there is no need to use a category. The quick fix would be to remove the category name from the parenthesis, transforming it into the class extension, then remove the class extension declaration from the .m file. After this you only import the extension header in your framework classes and you make sure it is a private header of your framework.

    MyClass.h

    @interface MyClass: NSObject
    
    @end
    

    MyClass+Internal.h

    #import "MyClass.h"
    
    @interface MyClass ()
    @property (nonatomic, copy) NSString *mySecretProperty;
    @end
    

    MyClass.m

    #import "MyClass.h"
    #import "MyClass+Internal.h"
    
    @implementation MyClass
    @end
    

    MyOtherClass.m:

    #import "MyClass.h"
    #import "MyClass+Internal.h"
    
    @implementation MyOtherClass
    - (void)test {
        MyClass *myClass = [MyClass new];
        NSLog(@"%@", myClass.mySecretProperty)
    }
    @end 
    

    The key is understanding the difference between categories and class extensions, see here: https://stackoverflow.com/a/4540582/703809

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