Objective C – Declared Properties

Property Declaration in Objective C

Prior to declare properties features in Objective C, developers were expected to declare member variables and implement the getter and setter methods.

With advent of the declare properties and synthesize this effort is no more required. But for memory management concepts, its key to know how properties work in Objective C

A sample getter setter implementation

//KSCustomClass.h
@interface KSCustomClass : NSObject
{
 NSString *firstName;
 NSString *lastName;
}
-(NSString *)firstName;
-(void)setFirstName:(NSString *)value;
-(NSString *)lastName;
-(void)setLastName:(NSString *)value;
//KSCustomClass.m
#import “KSCustomClass.h”
@implementation KSCustomClass
-(NSString *)firstName
{
 return firstName;
 //or return [[firstName retain] autorelease];
}
-(void)setFirstName:(NSString *)value
{
 if (firstName != value)
 {
 [firstName release];
 firstName = [value retain];
 }
}

With Declared Properties – the getter/setter methods would be synthesized or implemented automatically based on the attributes present in the @property directive.

The attributes help define the storage methodology and other behavior of the properties or member variables.

Attributes can be declared as

  • nonatomic or atomic (Mutually Exclusive)
  • readonly or readwrite (Mutually Exclusive)
  • retain or assign or copy or strong or weak (Mutually Exclusive)

Attribute Classification for Properties

Attributes are classifies based on

  • Writability 
    • readwrite
    • readonly
  • Semantics 
    • strong
    • weak
    • retain
    • assign
    • copy
  • Atomicity
    • nonatomic
    • atomic

 

1. Writability
The readwrite and readonly specifies if the property has a setter method or not.

Readwrite:

  • The readwrite attribute indicates if a property can be read and can be written into
  • If not specified, the default value is readwrite and both the getter and setter methods would be synthesized for the property

Readonly:

  • This attribute indicates that the property is read only and its value cannot be written into.
  • If the developer specifies readonly then only the getter method is synthesized automatically.
  • If a property is declared by attribute type as readonly and if we try to set its value using a direct assignment, we will get a compiler error

Sample Code:
@property (readonly) NSString *firstName;
@property (copy,readonly) NSString *firstName;
@property (retain,readonly) NSString *firstName;

2. Semantics

  • strong
  • weak
  • retain
  • assign
  • copy

The above attributes are Mutually exclusive i.e both copy and assign cannot be used on a property. Only one among these can be specified while declaring a property.

Retain

  • Gives ownership of the object to the class.
  • On using the retain attribute, the previous value is sent a release message and the new value is retained increasing the retain count by 1
  • Changes made to the object after it is retained will continue to reflect in the property
  • If a property is retained then it has to be released in the dealloc method

Sample Code:
@property (nonatomic, retain) NSString *firstName;
@synthesize firstName; //synthesize in the implementation

Alternative setter method if written without synthesize directive
-(void)setFirstName:(NSString *)value
{
 if (firstName != value)
 {
 [firstName release];
 firstName = [value retain];
 }
}

 Copy

  • Copy attribute gives ownership of the object to the class.
  • While calling the setter method a copy of the new value will be used and the older value will be released.

For e.g

  • If the property is a mutableArray and retain attribute is used for declaring the property, then post assignment any change to the mutableArray will continue to reflect in the new reference.
  • However if copy is used, then a copy of the NSMutableArray is created and any change in the original object will not reflect in the copied object.
  • Copy attribute increases the retain count of the object by 1
  • If a property is declared with copy attribute then it has to be released in the dealloc method

Sample Code:
If done using @property and @synthesize directive
@property (nonatomic, copy) NSString *firstName;
@synthesize firstName; //synthesize in the implementation

Alternative setter method if written without synthesize directive
-(void)setFirstName:(NSString *)value
{
 if (firstName != value)
 {
 [firstName release];
 firstName = [value copy];
 }
}

Assign

  • Uses simple assign on the property.
  • The assign attribute is the default attribute. i.e if no semantics is specified
  • The assign attribute should be used for float, bool, int (i.e scalars or primitive data types).
  • Any property that declares a non-owning relationship should be declared using assign & copy or retain should not be used while declaring the property
  • Any property which is declared with assign attribute must be set to nil in the dealloc method.

Sample Code:
If done using @property and @synthesize directive
@property (nonatomic, assign) int objectCount;
//@property (nonatomic) int objectCount; // If not stated assign is used by default
@synthesize objectCount; //synthesize in the implementation

Strong or Weak

  • In an ARC environment where the keyword retain cannot be used while declaring a property, strong is used instead.The attribute “Strong” specifies a owning relationship of the destination object, this behavior is similar to retain.
  • Strong attribute can be used in both ARC and non-ARC environments.
  • Similarly Weak is used for declaring property that represents a non-owning kind of relationship. The behavior of weak attribute is similar to that of assign.

3. Atomicity

Atomic & Non Atomic

  • There is no keyword “Atomic” to declare a property.
  • It is understood and implicit that if an object is not nonatomic, it is atomic in nature and thus it is threadsafe. i.e only one thread can operate on the property at any point of time.
  • Atomic is the default behavior and if the desired functionality is to allow multiple threads to operate on the same property then nonatomic attribute has to be explicitly stated.
  • Having an atomic attribute would be equivalent to writing a synchronized block in the setter method to make it thread safe

Reference

https://developer.apple.com/

Click to download the PDF Version of Knowledge Stack  – Cocoa_Declared_Properties 

 

 

Posted in Cocoa, iOS, Objective C Tagged with: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news