简体   繁体   English

Objective-C访问器声明(readonly,readwrite等)

[英]Objective-C accessor declarations (readonly, readwrite, etc)

In the book, "Cocoa Design Patterns," the author sometimes declares a property in the @interface as readonly: 在“可可设计模式”一书中,作者有时会在@interface中声明一个属性为readonly:

// .h
@property (readonly, copy) NSArray *shapesInOrderBackToFront;

and then later adds an unnamed category to the implementation (.m) file like this: 然后在实现(.m)文件中添加一个未命名的类别 ,如下所示:

// .m
@interface MYShapeEditorDocument ()
@property (readwrite, copy) NSArray *shapesInOrderBackToFront;
@end

Any idea as to why? 知道为什么? It's unclear to me how this approach is better than, or more necessary than, initially declaring the property as "readwrite". 我不清楚这种方法如何比最初将该属性声明为“readwrite”更好或更必要。

Externally the property will be readonly. 外部财产将是只读的。 While inside the class it will have both the accessor, and the setter. 在课堂内,它将同时具有访问者和设置者。

The setter will not be visible by the compiler outside of the implementation(.m) file. 编译器在实现(.m)文件之外将不会看到setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM