Objective-C访问器声明(只读,读写等) [英] Objective-C accessor declarations (readonly, readwrite, etc)

查看:72
本文介绍了Objective-C访问器声明(只读,读写等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在《可可设计模式》一书中,作者有时在@interface中将属性声明为只读:

In the book, "Cocoa Design Patterns," the author sometimes declares a property in the @interface as readonly:

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

,然后在实现(.m)中添加未命名类别像这样的文件:

and then later adds an unnamed category to the implementation (.m) file like this:

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

关于为什么的任何想法?对我来说,目前尚不清楚这种方法比最初将属性声明为 readwrite更好还是更必要。

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

推荐答案

在外部,该属性将为只读。在类内时,它将同时具有访问器和设置器。

Externally the property will be readonly. While inside the class it will have both the accessor, and the setter.

设置器在Implementation(.m)文件之外对编译器不可见。

The setter will not be visible by the compiler outside of the implementation(.m) file.

这篇关于Objective-C访问器声明(只读,读写等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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