Friday, September 28, 2012

A summary of property-properties attributes - and some tips


A summary of property-properties attributes - and some tips


@Property attributes
Writability
readwrite
mutually exclusive
default
Indicates that the property should be treated as read/write
readonly

only a getter method is required
if you attempt to assign a value using the dot syntax, you get a compiler error.
A readonly property can redelcare it as readwrite in a  class extension, a protocol or in a subclass.
Setter
Semantics
assign (iOS 5)
unsafe_unretain (iOS4)




default

setter uses simple assignment (new value not retained, old value is not released).
use this for scalar types (NSInteger, CGRect in Ref.Counted env.
on dealloc method make sure that object propertis marked assign are not released and the not assign are released.
property=newValue;
retain
strong
if (property!=newValue){
 [property release];
property = [newValue retain];
retain should be invoked on the object upon assignment
the previous value is sent a release message
valid only for Objective-C object types (no for Core Foundation)
_attribute_
use attribute keyword to specify that a Core foundation property should be treated like an Objective C for memory management
copy

if (property!=newValue){
[property release];
property = [newValue copy];
}
a copy of the object should be used for assignment
previus value is sent a release message
valid only for objects that implement the NSCopying protocol
WARNING-the copy method returns an immutable version of a collection, so if you want a mutable version you need to implement the setter rather to synthesize it.
Atomicity
nonatomic
Specify that accessor methods are not atomic
(by default accessors are atomic=robust access to properties in a multithreaded environment)
If you specity retain or copy with not the noatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value (similar to code below nonatomic)
[_internal lock]; //lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;
Interface Outlet
IBOutlet

To specify that a property is an Interface Builder oulet
@property (nonatomic, retain) IBOutlet NSButton *myButton;


  • when you see __Blabla is for instance variables and Blabla is for property attribute.
  • For declared properties, you should use assign instead of weak; for variables you should use __unsafe_unretained instead of __weak.
  • You will always use unsafe_unretained for properties that hold non-objects.
  • For properties that hold objects, you will usually use strong
  • unsafe_unretained will have a "dangling pointer" if the object it points to is deallocated.
  • Their most common use is for delegates, where you'd define a property for that delegate with the weak orunsafe_unretained attribute (assign is effectively unsafe_unretained),
  • copy attribute is most common with object types that have mutable subclasses.
  • copy method returns an immutable copy.
  • Curiously the copy and mutableCopy methods are define in NSObject like this:
-(id) copy              {return [self  copyWithZone:NULL];}
-(id) mutableCopy {return [self mutableCopyWithZone:NULL];}

         So you need to be carefully in your custom objects (you need to override the copyWithZone etc).

  •   nonatomic option will make your setter method run a tiny bit faster. 

TIP_1: 
For iOS 5.0-Lion use __weak. For iOS4.0-Snow Leopard use __unsafe_unretained. Why?

 As its name implies, __unsafe_unretained will continue pointing to the memory where an object was, even after it was deallocated. This can lead to crashes due to accessing that deallocated object.
Why would you ever use __unsafe_unretained then? Unfortunately, __weak is only supported for iOS 5.0 and Lion as deployment targets. If you want to target back to iOS 4.0 and Snow Leopard, you have to use the __unsafe_unretained qualifier.


TIP_2:
Do you know that when compiling for iOS or a 64-bit Mac OS X (not for 32-bit Mac OSX) program, you don't need to declare the instance variables?
The @property/@ synthesize calls are sufficient to make the space for the data.
(from The big nerd ranch Guide)

No comments:

Post a Comment