Database Reference
In-Depth Information
The compiler adds the retain in the setter because we specified it in the prop-
erty. If we had set it to assign instead, no retain would have occurred. Likewise,
the locking of the ivar is a default option that we could have turned off by
adding the nonatomic option to the property definition.
When dealing with multiple properties on an object, this can be a great time-
saver. There have also been indications that the accessors generated by the
compiler are faster than the “normal” accessors that developers write. In
addition to generating accessors, the @synthesize keyword is smart about what
it implements. If we need to implement our own setter for a property, it will
not overwrite that setter.
Alongside the @synthesize property, we have the @dynamic property. Unlike
@synthesize , which generates the accessors for us, @dynamic tells the compiler
that while the accessors for the property are not there at compile time, they
will be at runtime, and it instructs the compiler not to produce a warning for
them. @synthesize and @dynamic are sibling keywords. For each property, we
can use one or the other but not both. However, neither is required in a situ-
ation where we are implementing the accessors ourselves. If the accessor
methods will be implemented at runtime, we would use the @dynamic property
instead of the @synthesize property so that the compiler does not produce a
warning. This is particularly useful for Core Data subclasses, discussed in
Chapter 1, Under the Hood of Core Data , on page 1 .
It should be noted that it is possible to have one @property definition that does
not match the name of the ivar. For example, it is fairly common to have ivars
that start with an underscore, but the accessors do not include the under-
score. The @property can handle this as well as part of the @synthesize and
@dynamic calls.
@interface MyObject : NSObject
{
NSString *_myString; //Only required for 32-bit
}
@property (retain) NSString *myString;
@end
@implementation MyObject
@synthesize myString = _myString;
@end
Note that as of iOS 6.0 and Mac OS X 10.8, the ivar is different from the
property name by default. Unless you override the default, the ivar name will
have an underscore prefix.
 
 
Search WWH ::




Custom Search