iOS:使用@private来隐藏属性的设置
我正在编写一个类,它有很多我只想在内部使用的属性。这意味着我不希望用户在创建我的课程后能够访问它们。这是我在 .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;
我认为使用 @private
是我所需要的,但是也许我做错了。我的 .m 中是否还需要做一些事情?
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:
@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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@private
仅影响 ivars。不影响属性。如果您想避免将属性公开,请将它们放在类扩展中。在 .m 文件的顶部,放置类似“这看起来像一个类别”的内容,但类别“名称”为空。它称为类扩展,编译器将其视为原始 @interface 块的一部分。但由于它位于 .m 文件中,因此仅对该 .m 文件中的代码可见,而该文件外部的代码只能看到标头中声明的公共属性。
@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 likeThis 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.