iOS:使用@private来隐藏属性的设置

发布于 2024-12-11 20:11:31 字数 843 浏览 0 评论 0原文

我正在编写一个类,它有很多我只想在内部使用的属性。这意味着我不希望用户在创建我的课程后能够访问它们。这是我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-12-18 20:11:31

@private 仅影响 ivars。不影响属性。如果您想避免将属性公开,请将它们放在类扩展中。在 .m 文件的顶部,放置类似

@interface Lines ()
@property (nonatomic) CGRect originalFrame;
// etc.
@end

“这看起来像一个类别”的内容,但类别“名称”为空。它称为类扩展,编译器将其视为原始 @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 like

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文