理解 (readonly, copy)

Using Properties

Supported Types

You can declare a property for any Objective-C class, Core Foundation data type, or “plain old data” (POD) type (see C++ Language Note: POD Types). For constraints on using Core Foundation types, however, see “Core Foundation.”

Property Redeclaration

You can redeclare a property in a subclass, but (with the exception of readonly versus readwrite) you must repeat its attributes in whole in the subclasses. The same holds true for a property declared in a category or protocol—while the property may be redeclared in a category or protocol, the property’s attributes must be repeated in whole.

If you declare a property in one class as readonly, you can redeclare it as readwrite in a class extension (see “Extensions”), in a protocol, or in a subclass (see “Subclassing with Properties”). In the case of a class extension redeclaration, the fact that the property was redeclared prior to any @synthesize statement causes the setter to be synthesized. The ability to redeclare a read-only property as read/write enables two common implementation patterns: a mutable subclass of an immutable class (NSString, NSArray, and NSDictionary are all examples) and a property that has a public API that is readonly but a private readwrite implementation internal to the class. The following example shows using a class extension to provide a property that is declared as read-only in the public header but is redeclared privately as read/write.

// public header file


@interface MyObject : NSObject {


    NSString *language;


}


@property (readonly, copy) NSString *language;


@end


 


// private implementation file


@interface MyObject ()  // Class Extension must have no category name!


@property (readwrite, copy) NSString *language;


@end


 


@implementation MyObject


@synthesize language;


@end

猜你喜欢

转载自blog.csdn.net/microchenhong/article/details/6581639