iPhone Programming Part 3: Properties
It may seem quite absurd to focus on such simple things as getters and setters but when I started programming with objective-c it was a bit difficult for me to understand the behaviour of getters and setters. But don’t worry it is a simple overview – I will not go deep into it.
Manual Property Creation
1 2 3 4 5 6 7 8 9 10 11 | ///////////////////////////////// // File : example1.h @interface myClass { float value; } -(float) value; -(void) setValue:(float)_value; @end; |
1 2 3 4 5 6 7 8 9 10 11 | ///////////////////////////////// // File : example1.m @implementation myClass -(float) value { return value; } -(void) setValue:(float)_value { value = _value; } @end |
Automatic Property Creation 1
1 2 3 4 5 6 7 8 9 10 | ///////////////////////////////// // File : example2.h @interface myClass { float value; } @property float value; @end; |
1 2 3 4 5 6 7 8 9 10 11 | ///////////////////////////////// // File : example2.m @implementation myClass -(float) value { return value; } -(void) setValue:(float)_value { value = _value; } @end |
Automatic Property Creation 2
1 2 3 4 5 6 7 8 9 | ///////////////////////////////// // File : example3.h @interface myClass { float value; } @property float value; @end; |
1 2 3 4 5 6 | ///////////////////////////////// // File : example3.m @implementation myClass @synthesize value; @end |
Property Declaration Attributes
In some cases you must specify attributes to define how an external object accesses your property.
Accessor Method Names
To specify an accessor name you can use :
- getter = <getterName> – property is readable through the given getterName
- setter = <setterName> – property is writeable through the giben setterName
Writeability
To specify read and/or write accessors you can use :
- readwrite (default) — property is writeable and readable
- readonly — property is readonly
Setter Semantics
These attributes specify the semantics of a set accessor
- assign (default) — Specifies that the setter uses simple assignment.
- retain — Specifies that retain should be invoked on the object upon assignment. The previous value is sent a release message. This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects)
- copy — Specifies that a copy of the object should be used for assignment. The previous value is sent a release message. The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.
Atomicity (Multithread behaviour)
This attribute specifies that accessor methods are not atomic (Default is atomic).
- nonatomic — Specifies the accessors are not atomic
If your accessors are atomic – which is if you not specify them as nonatomic – a synthesized getter will look like :
1 2 3 4 | [_internal lock]; // lock using an object-level lock id result = [[value retain] autorelease]; [_internal unlock]; return result; |
If you specify nonatomic, then a synthesized accessor for an object property simply returns the value directly.
Interfacebuilder
If you want to specify that a property is an Interface Builder outlet, you can use IBOutlet keyword:
1 | @property (nonatomic, retain) IBOutlet NSButton *myButton; |
Synthesize
You can specify a variable to be used for a property:
1 | @synthesize firstName, lastName, age = yearsOld; |
Dynamic
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.
1 2 3 4 5 6 7 8 9 | ///////////////////////////////// // File : dynamic_example.h @interface MyClass : NSObject { NSString *value; } @property(copy, readwrite) NSString *value; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ///////////////////////////////// // File : dynamic_example.m // assume using garbage collection @implementation MyClass @dynamic value; - (NSString *)value { return value; } - (void)setValue:(NSString *)newValue { if (newValue != value) { value = [newValue copy]; } } @end |
Property access
When you access a property via synthesized dot notation or directly you have to notice the diffrence. If you access a property directly you will not use the accessor methods.
1 2 3 4 5 6 7 8 9 10 11 | ///////////////////////////////// // File : accessor.h @interface MyClass : NSObject { id value; } @property (nonatomic, retain) id value; -(void) myMethod; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | ///////////////////////////////// // File : accessor.m #import "accessor.h" @implementation MyClass @synthesize value; /* synthesize will do something like this : -(id) value { return value; } -(void) setValue:(id) _value { [value autorelease]; [_value retain]; value = _value; } */ -(void) myMethod { NSString * strValue = @"Hello Value"; // this would call the accessor 'setValue:( id );' // which will cause a retain on 'strValue' and a release // of what ever was there before. self.value = strValue; // the following call will just do that value now points to 'strValue', // but it does not call any accessor, so there will be no retain or release // message sent. value = strValue; id obj = nil; // just a holder for our object // the following line will call the accessor '( id ) value;' which will in // - in this case - do nothing special because the accessor just // returns the object. obj = self.value; // the following line will just do that obj has the same pointer as value // but will not call any accessor. obj = value; } @end |
About this entry
You’re currently reading “ iPhone Programming Part 3: Properties ,” an entry on coders
- Author:
- hhamm
- Published:
- 2.12.09 / 3pm
- Category:
- objective c, programming, xcode
- Feedback:


No comments
Jump to comment form | comments rss [?] | trackback uri [?]