Wednesday, December 8, 2010

Objective C properties


For those familiar with Object Oriented Programming the concept of getters and setters should be quite clear (thus skip the next paragraph).
Getters and Setters
Objects (instances of Classes) are usually referred to as binding of data with the functionality (or as of the year 2000 or so instead of functionality -”business logic“) related to processing this data. This data the object holds is the properties of that object. The classic example is if the object is a “car” than it has properties like “engine”, “steering wheel” and so on and also methods like “startEngine”, “steer to the left” and so on. In a classic setup for each data (property) of the class there would be 2 methods defined : one to set the value of the property (and check for correct data type of the value, if the value being passed is a number to check if it’s in the expected range and so fort) called setter, and one called getter – to get the value of the property.
Objective C Properties
Objective C is a very mature programming language, and also one that’s designed very very careful for the benefit of the programmer. Properties are a robust way to handle object’s data. In this article I can give just few quick examples over the basics, so let’s start.
First thing to know is that you need to define a property if you want a single piece of data (or a class object member) to be visible to other classes. In short if you want other classes to access your “car”s engine, you need to make it a property.
Definition of simple property:
@interface Tcm_properties : UIViewController {
 int count;
}
 
@property (readwrite) int count;
 
@end
So in the class interface you need 2 things:
  1. define an ivar (class variable) as you would normally do
  2. define a property using the directive @property
Did you notice the extra code (reading “readwrite”) between the parenthesis? This is a property attribute – here kicks the real Objective C magic – trough the property attributes you define how the property behaves – more about these in a moment.
After you define the property in the class interface, you need to also define the property implementation:
@implementation Tcm_properties
 
@synthesize  count;
 
@end
This is pretty much all the work. @synthesize will create automatically for you a setter and a getter methods for the property. The generated methods look like this (you never see this code, but this is what actually happens behind the curtains):
-(int)count {
 return count;
}
 
-(void)setCount:(int)value {
 count = value;
}
Here comes the great part, if you need a certain getter or setter to behave differenty you just need to override these methods. Like this :
-(void)setCount:(int)value {
 if (value>100) count = 100;
 else count = value;
}
Access properties vs. ivar access
You access the object’s properties by using the “.” notation. This is the only case in Objective C to use “.”
Accessing from within the class
self.count = 105 + value;
Accessing from another object
Tcm_properties* tcm = [[Tcm_properties alloc] init];
tcm.count = 1
So, keep in mind when you are done with the property if its value is an object you have to also release it (in your dealloc method is the right place).
-(void)dealloc {
 self.objProperty = nil
 [super dealloc]
}
Notice the little trick here instead of sending a release message to the property we assign it “nil”. Assigning nil to a retained property will also send a release message to the current value of the property.
Object property in a not thread concurrent setup
@property (nonatomic, retain) NSString* text;
This is probably the most common case you’d have, a property which is an object (NSString, NSArray or basically anything else which is not a simple number or a BOOL). The retain attribute, instructs the setter to retain the value when it’s being assigned and release the previous value if set. Great! This way you never use the value, since it’s count’s being increased by 1. Thenonatomic attribute, makes the compiler NOT check what other threads are executing over the value at the same time – what that means in simple words: if you are not doing some heavy thread work use nonatomic to speed up your code.
Non retained property value
@property (readwrite) float grade;
This case goes for all BOOL and int, float, NSInteger, CGPoint, CGSize and such. Use readwriteattribute to give write access to the property, or readonly to prevent overwriting of the property value.
Image: http://www.sxc.hu/profile/emmapayne

No comments:

Post a Comment

Followers