objective-c - iOS:使用@private 隐藏属性不被设置

标签 objective-c ios xcode private

我正在编写一个类,该类具有一堆我只想在内部使用的属性。这意味着我不想让用户在创建我的类(class)后访问它们。这是我的 .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 中是否也需要做些什么?

最佳答案

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

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

这看起来像一个类别,只是类别“名称”是空白的。它称为类扩展,编译器将其视为原始@interface block 的一部分。但是因为它在 .m 文件中,所以它只对该 .m 文件中的代码可见,它之外的代码只能看到在 header 中声明的公共(public)属性。

关于objective-c - iOS:使用@private 隐藏属性不被设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7909508/

相关文章:

ios - semanticContentAttribute 不起作用

javascript - iOS 上的 JQ UI 可拖动 : initiating dragging in taphold-handler

ios - 为什么 iOS UIWebView 的 loadRequest 方法没有返回值?

ios - UNUserNotificationCenter.current().getDeliveredNotifications 只返回一个空数组

iphone - 如何在 iPhone 的 View 顶部显示弹出窗口

iOS:通过应用程序测试互联网连接的最佳方式是什么?

ios - 优化工作流程以更新内部拥有的 cocoapods 依赖项?

ios - 购买应用内购买后保存更改

iphone - 如何将可变数组的对象以数组的形式存储到另一个可变数组中。?

iOS:如果语言环境合适,以 24 小时格式显示时间?