Property

You declare instance variables to hold the data, and you also declare accessor methods for them.

However, you can use @property/@synthesize to  construct instead.

@property (attributes) type name;

 

Mutability

A property can be declared readwrite or readonly. The default is readwrite, which means that both a setter and a getter method are created. If you don't want a setter method to be created, you mark the property as readonly.

Lifetime specifiers

A property can also be declared unsafe_unretained, strong, weak, or copy. This option determines how the setter handles its memory management.

unsafe_unretained is the default and the simplest: it just assigns the passed-in value to the property.

You will always use unsafe_unretained for properties that hold non-objects.

strong, will ensure that a strong reference is kept to the passed-in object. It will also let go of ownership of the old object( which will then deallocate itself if it has no other owners). For properties that hold objects, you will usually use strong.

weak does not imply owership of the object pointed to. It will synthesize a setter that sets the property to the passed-in object. If this object is deallocated, the property will be set to nil.(Note that if the pointer is unsafe_unretained and the object it points to is deallocated, you will have a "dangling pointer". Sending a message to a dangling pointer usually crashes your program.)

copy forms a strong reference to a copy of the object passed in. But,

@property (copy) NSString *lastName;
@synthesize lastName;

The generated setter method would look somewhat like this:

- (void) setLastName: (NSString *) d
{
    lastName =  [d copy];
}

 Use of the copy attribute is most common with object types that have mutable subclasses. For example, NSString has a subclass called NSMutableString.

//create a mutable string
NSMutableString *x = [[NSMutableString alloc] initWithString:@"Ono"];

//pass it to setLastName
[myObj setLastName:x];

//'copy' prevents this from changing the name

[x appendString:@" Lennon"];

More about copying

Curiously, the copy and mutableCopy methods are defined in NSObject like this:

- (id) copy
{
    return [self copyWithZone:NULL];
}

- (id) mutableCopy
{
    return [self mutableCopyWithZone:NULL];
}

about atomic 

Sadly, at this time, the default for properties is atomatic, so you do have to make this change

@property (copy, nonatomic) NSString *productName;
@property (nonatomic) int voltage;

 

Key-Value coding

key-value coding is the ability to read and set a property using its name. The key-value coding methods are defined in NSObject, and thus every object has this capability.

[a setProductName:@"Washing Machine"];
//rewrite using key-value coding

[a setValue:@"Washing Machine" forKey:@"productName"];

[a valueForKey:@"productName"];

Non-object types

The key-value coding methods are designed to work with objects, but some properties hold non-object type, like an int or a float. You use an NSNumber.

[a setValue:[NSNumber numberWithInt:240] forKey:@"voltage"];

 

dymanic

You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution. It suppresses the warnings that the compiler would otherwise generate if it can’t find suitable implementations. You should use it only if you know that the methods will be available at runtime.

NSManagedObject is provided by the Core Data framework. A managed object class has a corresponding schema that defines attributes and relationships for the class; at runtime, the Core Data framework generates accessor methods for these as necessary. You therefore typically declare properties for the attributes and relationships, but you don’t have to implement the accessor methods yourself and shouldn’t ask the compiler to do so. If you just declared the property without providing any implementation, however, the compiler would generate a warning. Using @dynamic suppresses the warning.

 

Note that you should always specify the copy attribute for an object property whose class conforms to the NSCopying protocol, even if garbage collection is enabled.

 

 

posted on 2012-06-11 14:45  grep  阅读(320)  评论(0编辑  收藏  举报