Wednesday, July 11, 2012

properties

Everybody uses iVars. You need them, that's no question. In order not to violate encapsulation either you have to provide accessors or you can declare properties. If you declare your variables as properties, you have to possibility to get your accessors generated by defined rules and set some other handy stuff.

Declaration

@interface someClass : NSObject {
    NSObject * someObject;
}
@property (nonatomic, retain, readwrite, getter = getMyAwesomeObject, setter = setNewAwesomeObject) NSObject * someObject;
@end

The result of this property declaration is something like this in the interface:
- (NSObject *) getMyAwesomeObject;
- (void) setNewAwesomeObject;

You can declare properties using the @property directive. Between the parentheses (which is an optional part) you can define attributes. These attributes will define how the accessors will work or how they should work if you implement them by yourselves.

Attributes

Accessor names
By using the getter and the setter attributes, you can define your accessors instead of the automatically generated accessors name (default in this case: getter=someObject, setter=setSomeObject).

Writability
By using readwrite, you have to have both accessors in your implementation and by using readonly you should have only a getter for your propert. The default writability is readwrite.

Setter mechanism

  • strong: specifies a "strong" relationship with the destination object. It kind of guarantees that your object won't be deallocated until you have a reference to it
  • weak: you won't have ownership over the value, if the object gets deallocated, your property's value will become nill
  • retain: upon setting a property like this, the setter should release the previous value and retain the new
  • assign: single assignment, no ownership
  • copy: previous value should be released, new should be copied

Synchronization
By using the atomic attribute you ensure that your property would be safely accessible in a multi thread environment during multiple accessing. The default attribute is atomic and you can use nonatomic to prevent this mechanism (you should avoid using atomic if you can, because it has robust mechanism).

IBOutlet
IBOutlet is an identifier for properties. It's purpose is to mark the property so that in the interface builder you can assign it to certain elements.

synthesize

This is the best part. Using @synthesize you don't have to create accessors it will be provided to you by the compiler. More than that if you have a property and you didn't declared your variable the compiler will do that as well. These accessors - generated by the compiler upon using @synthesize - will follow the policy given by the property declaration. For example if you set the attribute retain, your generated setter will release the previous entry than retain the new value, or if you used readonly, only a getter will be provided to you.

You can use the property=ivar formula to specify that a particular instance variable should have been used for the property.

@synthesize firstProperty, secondProperty, thirdProperty=someIvar;

Without using synthesize you must implement the accessors for the property and you should do the implementation so that it fits the property attributes.

Private & Public

By default all of your declared properties are public, so they are accessible from outside of your object. If you want to use properties but limit it's access, you have declare your private properties in the @private section of your interface.

@interface someClass : NSObject
@property (strong, nonatomic) NSObject * someObject;
@private
@property (strong, nonatomic) NSString * somePrivateProperty;
@end

That's all i have for now, hope it will help understand the basics.

No comments:

Post a Comment