iOS:使用@private隐藏设置的属性 [英] iOS: Using @private to hide properties from being set

查看:105
本文介绍了iOS:使用@private隐藏设置的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拧一个有一堆我想在内部使用的属性的类。这意味着我不希望用户在创建我的类时访问它们。这是我在.h中的内容,但它仍然没有隐藏在XCode中的自动完成菜单(点击转义以查看列表)中的那些:

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;

我认为使用 @private 是什么我需要,但也许我做错了。是否还需要在我的.m中完成某些事情?

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 仅影响ivars。它不会影响属性。如果您想避免公开属性,请将它们放在类扩展中。在.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

这看起来像一个类别,除了类别名称为空。它被称为类扩展,编译器将其视为原始@interface块的一部分。但由于它位于.m文件中,因此只有.m文件中的代码才可见,并且其外部的代码只能看到标题中声明的公共属性。

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.

这篇关于iOS:使用@private隐藏设置的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆