我正在创建一个静态库以使用以下指南进行共享: http://www.amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html
在其中一个函数中,我返回一个“SomeUIView”,它是 UIView 的子类,并在公共(public) header 中定义,但是我不想在公共(public) header 中公开 SomeUIView 的内部实例变量。
我尝试使用 SomeUIView 的私有(private)内部头文件的类别,但我不断遇到“类‘SomeUIView’的重复接口(interface)声明”。
有人知道怎么做吗?
谢谢!
最佳答案
类别和扩展不能向类添加实例变量。我会在这里使用 PIMPL 习惯用法 - 使用私有(private)实现对象:
// header
@class MyObjImpl;
@interface MyObj {
MyObjImpl* impl;
}
@end
// implementation file:
@interface MyObjImpl {
id someIvar;
}
// ...
@end
// ... etc.
这还可以使您的公共(public)界面保持稳定,以防您想添加一些内容供内部使用。
“重复接口(interface)”来自第二个接口(interface)声明中缺少的括号:
// header:
@interface MyObj
// ...
@end
// implementation file:
@interface MyObj () // note the parentheses which make it a class extension
// ...
@end
关于iPhone静态库: How to hide instance variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2909392/