iPhone静态库:如何隐藏实例变量
我正在创建一个静态库以使用以下指南进行共享: http://www.amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html" amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html
在其中一个函数中,我返回一个“SomeUIView”,它是 UIView 的子类并被定义在公共标头中,但是我不想在公共标头中公开 SomeUIView 的内部实例变量。
我尝试过使用 SomeUIView 的私有内部头文件的类别,但我一直遇到“类‘SomeUIView’的重复接口声明”。
有谁知道该怎么做?
谢谢!
I'm creating a static library to share using the following guide:
http://www.amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html
In one of the functions, I return a "SomeUIView" which is a subclass of UIView and is defined in the public header, however I don't want to expose the internal instance variable of SomeUIView in the public header.
I've tried using categories for a private internal header file for SomeUIView, but I keep running into "Duplicate interface declaration for class 'SomeUIView'".
Does anyone know how to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类别和扩展不能向类添加实例变量。我会在这里使用 PIMPL 习惯用法 - 使用私有实现对象:
这也可以使您的公共接口保持稳定,以防您想添加一些内容供内部使用。
“重复接口”来自第二个接口声明中缺少的括号:
Categories and extensions can't add instance variables to a class. I'd go for the PIMPL idiom here - use a private implementation object:
This also keeps your public interface stable in case you want to add something for internal use.
The "duplicate interface" comes from missing parentheses in the second interface declaration:
您还可以使用 Objective-C 2 称为“关联引用”的功能。
这并不是真正的面向对象的 API,但是您可以通过使用运行时的一些简单函数来向另一个对象添加/删除对象:
设置值或在值为 nil 时删除它。
检索指定键的值。
请注意,这也是在实现类别时向现有对象添加“实例变量”的方法。
关键是指向私有变量的简单指针,您可以使用以下方法将其声明为模块私有:
You may also use the Objective-C 2 feature known as "Associative reference".
This is not really object-oriented API, but you can add/remove object to another object by using some simple functions of the runtime:
Sets the value or remove it when value is nil.
Retrieve the value for specified key.
Note that this is also a mean to add "instance variable" to existing object when implementing a category.
Key is s simple pointer to private variable that you can declare as a module private by using: