objective-c - Objective-C : where to declare private instance properties?

标签 objective-c implementation private class-extensions

我有以下类接口(interface):

@interface MyClass : NSObject

@property int publicProperty;

@end

然后是实现:

@interface MyClass() // class extension

- (void)privateMethod; // private methods

@end

@implementation MyClass {
    int _privateProperty;
}

@property int privateProperty = _privateProperty;

@end

这是 Apple 人在 WWDC 上展示的内容,但是有什么理由不将 _privateProperty 放在类扩展中,例如:

@interface MyClass() // class extension
{
    int _privateProperty;
}

- (void)privateMethod; // private methods

@end

谢谢!

最佳答案

我通常在实现中通过扩展“强制”私有(private)

在你的标题中

@interface MyClass : NSObject
{
}

@property (nonatomic, assign) int publicProperty;

@end

在你的实现文件中:

@interface MyClass ()
@property (nonatomic, assign) int privateProperty;
@end


@implementation MyClass
@synthesize privateProperty;
@synthesize publicProperty;

@end

关于objective-c - Objective-C : where to declare private instance properties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11304327/

相关文章:

ios - DrawRect:如何在给定一组有序点的情况下填充多边形?

iPhone:viewDidDisappear 的正确用法是什么?

java - 子类构造函数是否从父类(super class)构造函数继承变量?

ios - 向 iOS 设备分发证书和私钥

javascript - javascript中的嵌套类,私有(private)方法的继承

objective-c - Objective-C 中的 Mime 编码字解析

用于解压缩文件的 iOS native 类或库?

delphi - 实现多个接口(interface)的类的清晰度(委托(delegate)的替代方案):

python - 在 Python 中实现 lisp

c - C中B+树的简单实现