隐藏公共框架标头中的属性,但内部保留可用 [英] Hiding properties from public framework headers, but leaving available internally

查看:93
本文介绍了隐藏公共框架标头中的属性,但内部保留可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在类中具有从公共框架头中排除的属性,但可以在其他框架类中内部使用. 我现在所做的是:

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:

MyClass.h:

MyClass.h:

@interface MyClass: NSObject

@end

MyClass + Internal.h

MyClass+Internal.h

@interface MyClass (Internal)
@property (nonatomic, copy) NSString *mySecretProperty;
@end

MyClass.m

MyClass.m

#import "MyClass.h"
#import "MyClass+Internal.h"

@interface MyClass ()
@property (nonatomic, copy) NSString *mySecretProperty;
@end

@implementation MyClass
@end  

我可以使用私有财产,例如:

And I can use private property like:

MyOtherClass.m:

MyOtherClass.m:

#import "MyClass.h"
#import "MyClass+Internal.h"

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

但是,我对此设置不满意的是,我在Internal类别和匿名类别中有重复的属性声明. 有没有办法改善此设置?

But what I don't like about this setup is that I have duplicate declaration of property in my Internal Category and inside of anonymous Category. Is there a way to improve this setup?

推荐答案

我认为您只能使用类扩展名,而无需使用类别.快速解决方案是从括号中删除类别名称,将其转换为类扩展名,然后从.m文件中删除类扩展声明. 之后,只需将扩展头导入框架类中,并确保它是框架的私有头.

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 

关键是要了解类别和类扩展之间的区别,请参见此处: https://stackoverflow.com/a/4540582/703809

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

这篇关于隐藏公共框架标头中的属性,但内部保留可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆