今天讲Objective-C的属性访问控制
Property Attributes Indicate Data Accessibility and Storage Considerations
The examples shown so far all declare properties that are intended for complete public access. This means that other objects can both read and change the values of the properties.
In some cases, you might decide to declare that a property is not intended to be changed. In the real world, a person must fill out a large amount of paperwork to change their documented first or last name. If you were writing an official record-keeping app,
you might choose that the public properties for a person’s name be specified as read-only, requiring that any changes be requested through an intermediary object responsible for validating the request and approving or denying it.
Objective-C property declarations can include property attributes, which are used to indicate, among other things, whether a property is intended to be read-only. In an official record-keeping app, the Person class interface might look like this:
@interface Person : NSObject
@property (readonly) NSString *firstName;
@property (readonly) NSString *lastName;
@endProperty attributes are specified inside parentheses after the @property keyword, and are described fully in Declare Public Properties for Exposed Data.
本文讲解了Objective-C中如何使用属性访问控制来指定属性为只读或可读写,通过实例说明了在官方记录保存应用中如何声明姓名等属性为只读。
235

被折叠的 条评论
为什么被折叠?



