Wednesday, June 15, 2011

Property Attribute for iphone App

The following are some attribute used for property definition:

[assign]
This attribute indicates the property uses assignment in the property’s setter. No release to be called to the old value, and no retain to be called on the new value.

[retain]
This attribute indicates the property calls release to the old value, and retain on the new value.

[copy]
This attribute indicates the property calls release to the old value, and then allocate and copy a new object to the new value. The copy operation will retain on the newly created object.

[nonatomic]
This attribute indicate that the property’s accessor methods are not thread safe.. So do not access this property from multi-threads

When the variable or data member needs to be released for iPhone

Basically, all variable and data members that is derived from NSObject needs to be explicitly release in iphone applications. When the variable or data member goes out of scope, the application or OS will not release it automatically, as all variable or data members are allocated in heap instead of stack. There is no concept of stack in objective C, so none of the variable or data member can be cleaned up automatically.

For local variable of NSObject in a method, releasing the variable before exiting the method.

For property, as the setter will release its current property value and retain the new value, so need to release the property value when the object instance is deallocated.

For instance variable, same as property, release the value when the object instance is deallocated

Certainly, if the variable or variable is changed in the middle, the old value needs to be released and the new value needs to be retained.

There are some exceptions due to compiler optimization, particularly for NSString objects, which may cause confusion to developers. For example, NSString object created with
[[NSString alloc] initWithString@"abc"] or [NSString StringWithString] has a retain count of 2147483647 instead of 1. The reason is the returned NSString just points to immutable constant NSString object passed in, which does not need to be released. However, NSString object created with [[NSString alloc] initWithUTF8String:"abc"] will need developer to release it. The IOS reference document does not mention anything about this kind of difference, and the only clue available is checking retainCount of the returned object.
In addition, based the memory management rules from Apple, release method should be called for [[NSString alloc] initWithString@"abc"], althoug we know actually it does nothing.

- You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.

- You use release or autorelease to relinquish ownership of an object. autorelease just means “send a release message in the future” (specifically: when the used autorelease pool receives a drain message—to understand when this will be, see “Autorelease Pools”).