Declared Properties

Writability

These attributes specify whether or not a property has an associated set accessor. They are mutually exclusive.

readwrite

    Indicates that the property should be treated as read/write. This is the default.

    Both a getter and setter method will be required in the @implementation. If you use @synthesize in the implementation block, the getter and setter methods are synthesized.
readonly

    Indicates that the property is read-only.

    If you specify readonly, only a getter method is required in the @implementation. If you use @synthesize in the implementation block, only the getter method is synthesized. Moreover, if you attempt to assign a value using the dot syntax, you get a compiler error.

Setter Semantics

These attributes specify the semantics of a set accessor. They are mutually exclusive.

assign

    Specifies that the setter uses simple assignment. This is the default.

    You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own such as delegates.

    retain and assign are effectively the same in a garbage-collected environment.
retain

    Specifies that retain should be invoked on the object upon assignment. (The default is assign.)

    The previous value is sent a release message.

    Prior to Mac OS X v10.6, this attribute is valid only for Objective-C object types (so you cannot specify retain for Core Foundation objects—see “Core Foundation”).

    On Mac OS X v10.6 and later, you can use the __attribute__ keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management, as illustrated in this example:

    @property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;

copy

    Specifies that a copy of the object should be used for assignment. (The default is assign.)

    The previous value is sent a release message.

    The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol. For further discussion, see “Copy.”

Different constraints apply depending on whether or not you use garbage collection:

    *

      If you do not use garbage collection, for object properties you must explicitly specify one of assign, retain or copy—otherwise you will get a compiler warning. (This encourages you to think about what memory management behavior you want and type it explicitly.)

      To decide which you should choose, you need to understand Cocoa’s memory management policy (see Memory Management Programming Guide).
    *

      If you use garbage collection, you don't get a warning if you use the default (that is, if you don’t specify any of assign, retain or copy) unless the property's type is a class that conforms to NSCopying. The default is usually what you want; if the property type can be copied, however, to preserve encapsulation you often want to make a private copy of the object.

你可能感兴趣的:(C++,c,cocoa,Objective-C,OS)