问题描述
我已经浏览了很多关于这个主题的帖子。也许我没有碰到那个,有人会指出我的方向。问题很简单,可能有一个简单的答案。
I've looked through a bunch of posts on this subject. Maybe I didn't run across "the one" and someone will point me in that direction. The question is simple and probably has a simple answer.
如果你有两个ivars,比如说,public_ivar和private_ivar,你应该在哪里/怎么声明它们什么是公共的,什么是私有的,不会以任何方式暴露给任何查看头文件的人?
If you have two ivars, say, "public_ivar" and "private_ivar", where/how should you declare them so that what is public is public and what is private is not exposed in any way to anyone looking at the header file?
public_method和 private_method。
Same question in the case of "public_method" and "private_method".
我喜欢干净的头文件(在其他语言中),它只暴露我希望别人看到的方法和ivars。您应该能够发布您的头文件,而不会遇到某人访问他们不应该访问的东西的危险。你如何在Objective-C中做到这一点。
I like clean header files (in other languages) that only expose the methods and ivars I want someone else to see. You should be able to publish your header file and not run into the danger of someone accessing something they are not supposed to. How do you do that in objective-C.
例如,假设我决定我需要使用ivar来跟踪一些数据,一个计数器或这样的事情,在所有需要访问这些信息的类方法之间。如果在@interface下的标题中传统地声明了这个ivar,那么它的存在是公开通告的,任何创建该类实例的人都可以使用它。理想情况是这个ivar在类实现之外根本不可见。
For example, let's say that I decide that I need to use an ivar to keep track of some data, a counter or somthing like that, between various class methods that all need access to this information. If that ivar is declared conventionally in the header under @interface its existence is publicly advertised and it is usable by anyone creating an instance of the class. The ideal scenario would be that this ivar would not be visible at all outside of the class implementation.
推荐答案
你可以声明实例变量或类扩展中声明的属性。由于类扩展是在实现文件中声明的(即,不是头文件),因此检查头文件的人不会看到它们。例如,在头文件中:
You can declare instance variables or declared properties in a class extension. Since a class extension is declared in an implementation file (i.e., not a header file), they won’t be visible to someone inspecting the header file. For instance, in the header file:
@interface SomeClass : NSObject
@end
并在实施文件中:
@interface SomeClass ()
@property (nonatomic, assign) int privateInt;
@end
@implementation SomeClass
@synthesize privateInt;
…
@end
或
@interface SomeClass () {
int privateInt;
}
@end
@implementation SomeClass
…
@end
请注意,在运行时期间没有任何内容阻止访问私有/类扩展实例变量(或类扩展中声明的属性的访问器方法)。我写了一篇相当详细的帖子,作为Stack Overflow上另一个问题的答案:
Note that there’s nothing preventing access to private/class extension instance variables (or the accessor methods for properties declared in a class extension) during runtime. I’ve written a rather detailed post about this as an answer to another question on Stack Overflow: Does a private @property create an @private instance variable?
编辑:类扩展中的实例变量在WWDC 2010会话144中提供。
Instance variables in class extensions were presented in WWDC 2010 session 144.
编辑:使用Clang / LLVM 2.0编译器,您还可以在类扩展中声明属性和实例变量。
"Using the Clang/LLVM 2.0 compiler, you can also declare properties and instance variables in a class extension."
这篇关于如何声明在类实例外部不可见或不可用的实例变量和方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!