Objective C – Some basics

If you are developing for the iOS platform, its invaluable to have foundational knowledge in Objective C. This brief blog posts covers some fundamental terms in objective C, hope you will find it useful.

1. Protocols – Protocols in objective C are equivalent to Interfaces in Java. They contain method definitions, either required or optional methods, which the implementing class implements. A good example is UITableViewDataSource Protocol. A class can implement multiple Protocols. For more details, refer to :


2. Categories – A category allows you to add methods to an existing class even if you do not have a the source code available for that class! Categories offers a neat alternative to subclassing by allowing you to extend the functionality of a class. For example, to add functionality to class named MyCategoryClass you can write the following code:

#import “MyCategoryClass.h”

@interface MyCategoryClass (CategoryName)

//method declarations



The category name indicates that the methods are additions to a class declared elsewhere, not a new class. Note that you cannot use a category to add additional instance variables to a class.

3. Properties – Properties provide a simple way to declare and implement an object’s accessor methods. There are two key parts to understanding properties in objective C, declaration and implementation.

You would normally declare a property in class header file.

Property Attributes —

Mutability – readwrite or readonly, default is readwrite

e.g. @property (readonly) int voltage;

Lifetime Specifiers — unsafe-unretained (default) e.g. @property int voltage (voltage in this case is unsafe unretained)

strong — ensures that a strong reference or ownership is kept to the passed-in object.

weak — does not imply ownership of the object pointed to. E.g. in case of parent child relationship, parent should have a strong reference to child, however, the child should have a weak reference. So that when child is deallocated, the parent reference to child can be released.

copy — in this case the setter will make a copy of the new object passed and set the pointer to point to the copied object.

e.g. @property (copy) NSString *serialNumber;

– (void) setSerialNumber: (NSString *) newSerialNumber {

newSerialNumber = [newSerialNumber copy];

[serialNumber release]; // releasing the original pointer

serialNumber = newSerialNumber;


4. Key Value Coding

KV is the ability to set and read property using its name. e.g. if we have a property name productName declared in a class whose instance is ‘a’, we can use the following code:

[a setValue@”Washing Machine” forKey@”productName”];





  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: