Objective-C:如何向类外隐藏类成员?
我正在与某些事情作斗争,但找不到任何令人满意的解决方案。
我有一个带有“myMutableArray”成员的类。
我希望该类能够自行管理从数组中添加和删除项目,因此我不希望任何其他类能够访问该成员并调用其上的 NSMutableArray 方法。
在理想情况下,我希望该成员有一个私有 getter(以便能够调用 self.myMutableArray)和一个公共 setter。
你知道我如何实现这一目标吗?
换句话说:
我希望其他类
能够调用
- [oneInstance setMyMutableArray:thisArray]; // set
- oneInstance.myMutableArray = thisArray; // set using setter
- thisArray = oneInstance.myMutableArray; // get
- [oneInstance addItem:anItem]; // add
无法调用:
- [oneInstance.myMutableArray add:etc...] // add
我希望我的类
能够调用
- self.myMytableArray = [NSMutableArray array]; // set
- thisArray = self.myMytableArray ; // get
谢谢。
I'm fighting with something and I don't find any satisfying solution.
I have a class with a "myMutableArray" member.
I would like the class to manage itself adding and removing items from the array, so I don't want any other class being able to access the member and call NSMutableArray methods on it.
In an ideal situation, I would like to have a private getter (to be able to call self.myMutableArray) and a public setter for this member.
Do you know how I may achieve this ?
In other words :
I would like other classes
be able to call
- [oneInstance setMyMutableArray:thisArray]; // set
- oneInstance.myMutableArray = thisArray; // set using setter
- thisArray = oneInstance.myMutableArray; // get
- [oneInstance addItem:anItem]; // add
not being able to call :
- [oneInstance.myMutableArray add:etc...] // add
I would like my class
be able to call
- self.myMytableArray = [NSMutableArray array]; // set
- thisArray = self.myMytableArray ; // get
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有什么理由需要公共设置器吗?听起来好像类本身拥有该数组。您可能最好不要提供对该字段的任何公共属性访问权限,并创建一个将值复制到私有字段中的公共方法。
Is there any reason you need the public setter? It sounds like the class itself owns the array. You'd probably be better off not providing any public property access to the field, and making a public method which copies the values into your private field.
Foo.h
Foo.m
一些细节:
Objective-C 有“实例变量”,而不是“成员变量”。
上面定义了自动合成的公共 getter 和私有 setter。为了清楚起见,我还添加了一个公共方法和一个私有方法。
Objective-C 中的“公共”和“私有”完全由编译器的可见性定义。
myMutableArray
的 setter 和方法doSomethingInPrivate
只是私有的,因为它们在@interface
中的声明无法导入。self.myMutableArray
和[self myMutableArray]
做同样的事情;.
语法仅仅是等效方法调用的简写(除了这个问题之外还有一些边缘情况细节)@interface< 中的
@property
/code> 纯粹是方法声明的简写(带有一些额外的元数据)。@interface Foo()
是一个类扩展,而不是类别。它的存在正是出于上述目的;使用范围应受到限制的附加声明信息来扩展类的@interface
。它可以出现在头文件中,也就是说,您只需导入库的实现即可创建库私有功能。@synthesize
和@property
也不提供常规方法实现时,则使用@dynamic
。 否则不需要!我可能忘记了什么。
Foo.h
Foo.m
Some details:
Objective-C has "instance variables", not "member variables".
The above defines a public getter and private setter that is synthesized automatically. For clarity's sake, I also added a public method and a private method.
"Public" and "private" in Objective-C are defined entirely by visibility to the compiler. The setter for
myMutableArray
and the methoddoSomethingInPrivate
are only private because their declarations in an@interface
cannot be imported.self.myMutableArray
and[self myMutableArray]
do the same thing; the.
syntax is merely short hand for an equivalent method call (with a few edge case details beyond this question)@property
in the@interface
is purely short hand for method declarations (with a bit of extra metadata).@interface Foo()
is a class extension and not a category. It exists for exactly the purpose demonstrated above; to extend the@interface
of a class with additional declarative information whose scope should be limited. It can appear in a header file that, say, you only import in your library's implementation to create library-private functionality.@dynamic
is used when you neither@synthesize
an@property
nor provide a conventional method implementation. It is not needed otherwise!I'm probably forgetting something.