xcode iOS:使用@private 隐藏设置的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7909508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Using @private to hide properties from being set
提问by Nic Hubbard
I am wring a class that has a bunch of properties that I want to only use internally. Meaning I don't want to be able to have a user access them when they have created my class. Here is what I have in my .h but it still doesn't hide those from the autocomplete menu (hitting escape to see list) in XCode:
我正在编写一个具有一堆我只想在内部使用的属性的类。这意味着我不希望用户在创建我的类时访问它们。这是我在 .h 中的内容,但它仍然没有从 XCode 中的自动完成菜单(点击转义以查看列表)中隐藏那些:
@interface Lines : UIView {
UIColor *lineColor;
CGFloat lineWidth;
@private
NSMutableArray *data;
NSMutableArray *computedData;
CGRect originalFrame;
UIBezierPath *linePath;
float point;
float xCount;
}
@property (nonatomic, retain) UIColor *lineColor;
@property (nonatomic) CGFloat lineWidth;
@property (nonatomic) CGRect originalFrame;
@property (nonatomic, retain) UIBezierPath *linePath;
@property (nonatomic) float point;
@property (nonatomic) float xCount;
@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) NSMutableArray *computedData;
I thought that using @private
was what I needed, but maybe I have done it wrong. Does something need to also be done in my .m?
我认为使用@private
是我需要的,但也许我做错了。是否还需要在我的 .m 中做一些事情?
回答by Lily Ballard
@private
only affects ivars. It doesn't affect properties. If you want to avoid making properties public, put them in a class extension instead. In your .m file, at the top, put something like
@private
只影响ivars。不影响属性。如果您想避免公开属性,请将它们放在类扩展中。在您的 .m 文件中,在顶部放置类似
@interface Lines ()
@property (nonatomic) CGRect originalFrame;
// etc.
@end
This looks like a category, except the category "name" is blank. It's called a class extension, and the compiler treats it as if it were part of the original @interface block. But since it's in the .m file, it's only visible to code in that .m file, and code outside it can only see the public properties that are declared in the header.
这看起来像一个类别,除了类别“名称”为空。它被称为类扩展,编译器将其视为原始@interface 块的一部分。但是由于它在 .m 文件中,因此它只能对 .m 文件中的代码可见,而它之外的代码只能看到在标头中声明的公共属性。