Propertys for accessors
Encapsulation is one of the most important concepts in object-oriented programming. To ensure Encapsulation, setter and getter methods (accessors) have to be written for every instance-variable. This could be very time-consuming and boring, because the most accessors are quite the same, except the instance-variable name. Therefore the decision is obvious to let the compiler generate accessors for us. This proceeding is called synthesize and was implemented with Objective-C 2.0 with so called properties.
Propertys generel syntax:
header
@property (attribute1, attribute2, attribute3, attribute4) datatype variablename;
implementation
@syntesize variablename;
If the instance-variable is synthesized, the access is:
standard-syntax
[Obj setVariablename:value]
printf(„The Value: %i“, [Obj variablename]);
point-syntax
Obj.variablename = value;
printf(„The Value: %i“, Obj.variablename);
Annotation:
The Dot-Syntax exists without dependence on properties. Its possible to write accessors without properties and access them via Dot-Syntax.
The 4 attributes define following:
-access-type (readonly, readwrite)
-setter-semantik (assign, retain, copy)
-atomicity
-setter- and getter-name
Access-type
In case of readonly only the getter is synthesized. With readwrite both, getter and setter, are synthesized.
Setter-semantic
Assign
Assign is the standard setter-semantic. If you leave the setter-sematic attribute empty, the compiler choose automatically the assign semantic.Assign is the simplest setter-semantic. The new value is simply assigned to the instance-variable, nothing else. The reference-counter is not increased and thereby there is no need to decrease an older increasement. The consequence is, that the receiver-class isnt an object-owner. If the sender-class decrease the reference-counter, the object will be deleted and the receiver-class no longer has access to it. Offcourse this could happen inversely, too. Because of this, propertys with assign-semantic shouldnt be released in the dealloc-method (because its never been retained).
Example (compiler-generated code)
-(void)setVariablenname:(id)senderValue
{
instanceValue = senderValue;
}

Retain
Retain solves the problem of the assign-semantik. First the instance-variable is released, because it maybe points already to an other object. Then the new value is assigned to the instance-variable. In the last step the instance-variable is retained. This means that the receiver-class is now an object-owner. The sender-class is now free to release the object and it still stays alive and accessible to the receiver-class. As the retain-semantic retains the instance-variable, it has to be released in the dealloc-method. Both classes (receiver and sender) point to the same object. If one of the classes change the object it is also changed to the other class.
Example (compiler-generated code)
-(void)setVariablenname:(id)senderValue
{
if(instanceValue != senderValue)
{
[instanceValue release];
instanceValue = senderValue;
[instanceValue retain];
}
}

Copy
Copy allows a class to be the only owner of an object. The submitted object is first copied and then assigned to the instace-variable. The advantage is the independance of the object. If the sender-class changes the object, it has no consequences to the copied object from the receiver-class.
Example (compiler-generated code)
-(void)setVariablenname:(id)senderValue
{
if(instanceValue != senderValue)
{
[instanceValue release];
instanceValue = [senderValue copy];
}
}

Atomicity
Atomicity assure safe access of multiple threads. Only one thread is allowed to access the accessors at the same time.To avoid this the attribute „nonatomic“ has to be named. More at friday.com
Setter- and getter-names
As described above the standard names are „setVariablename:“ (setter) and „variablename (getter). This can be changed. As example, in case of boolean-variables, its nice to have a getter like „isInitial“. To manage this the attribute „getter = isInitial“ have to be named in the property definition. Thereby the boolean-variable can be accessed like this:
[Object isInitial];
Object.isInitial;
If the access-type is „readwrite“, its also possible to change the standardame for the setter-method with the attribute „setter = otherName“.
Source
developer.apple.com

English
Deutsch





















