13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2: Object-Oriented Programming 37Properties in <strong>Objective</strong>-C can also accept a number of qualifiers that define how they can beused. These are placed in parentheses following the @property keyword and includethe following:• Access (readonly, readwrite): Denotes whether a property is settable orread-only. The default is readwrite. Only one of these may be specified on asingle property.• Thread-safety (atomic, nonatomic): By specifying the atomic keyword (thedefault) all synthesized accessors for this property will be locked andsynchronized for thread-safety. The nonatomic keyword disables this andis commonly used on iOS where large amounts of locking can degradeperformance.• Storage (assign, retain, copy, strong, weak): Scalar variable typesdefault to the assign storage type, while objects default to using retainto increment the value’s reference count (and similarly release it when thevalue is changed or unset). If an object value supports it, you can use copyto indicate that the object should be copied wholesale, not simply retained(useful for mutable values). The strong and weak qualifiers are new withARC: the former denotes a strong (retained) reference to an object, whilethe latter is a non-retained zeroing reference. If the value is deallocated, thevalue of the property is automatically set to nil. Only one of these may bespecified for a single property.• Methods (getter=, setter=): These options allow the specification of custommessage selectors for a property. By default, a property named myPropertywould have a getter method named myProperty and a setter namedsetMyProperty:. This is most commonly used for Boolean properties: aproperty hidden could use isHidden and setHidden: as its methods.Some more idiomatic property declarations can be seen in Listing 2-10.Listing 2-10. More Idiomatic Property Declarations@interface MyObject : NSObject@property (nonatomic, assign, getter = isHidden) BOOL hidden;@property (weak) id myParent;@property (nonatomic, copy) String * title;@endSince property declarations carry a lot of information, the compiler can synthesize everythingabout them, including the accessor methods and the instance variables backing them. Asa result, if a class’s data members are intended to be accessible, it is common (and indeedidiomatic) to declare them using only the property syntax and let the compiler worry about theimplementation details.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!