By A Beginner

Every Expert Was Once A Beginner

Protocols in Objective – C — July 6, 2015

Protocols in Objective – C


As we have interfaces in Java and C#, and like abstract class in C++ similarly in Objective – C we have ‘protocols‘. A protocol is a list of methods that your confirming class promises to implement. It is not a real class, It can only declare methods and cannot hold any data. We can use Protocols to mark classes as having certain capabilities, like the ability to be copied, to be serialized and deserialized, or to act as a data source for some other class. Protocols make it possible for two classes distantly related by inheritance to communicate with each other to accomplish a certain goal. Thus protocols are used as an alternative to the subclassing.

Declaring Protocols

To declare a custom protocol we can use the below syntax.  A protocol can be declared in the ‘.h’ file of a class before the interface declaration.

@protocol MyProtocolName

//My protocol methods declaration


You can mark a class as conforming to above protocol by declaring so in the class’s interface:

@interface SomeObject : NSObject 


Here the class ‘SomeObject’  conforms to the protocol ‘MyProtocolName‘, thus the class ‘SomeObject’ must have to implement all the methods declared in the conforming protocol.

Protocols are not same as subclassing a class, where your class inherits number of methods and can choose to override some or all of them. Where as, conforming to the protocol means that you must implement all of the required methods that the protocol specifies.

To create an object conforming to the protocol we use the syntax as below:

@property(weak) id someConformingObject;

Here the object ‘someConfirmingObject’ confirming to ‘MyProtocolName’ is of type ‘id , so that it could be an object of any class at the run-time, but it is guaranteed that it implements the methods listed in the protocol.

Example for implementing custom Protocol:

Consider the two classes classA and classB , where classB contains the protocol called ‘MyProtocol‘. Here my classA has the strong reference to the object of classB and it conforms to the protocol ‘MyProtocol‘. In classB we have an object delegateObj of type id conforming to the protocol MyProtocol, this object holds the weak reference to the object of classA. The implementation for the methods in the protocols will be found in classA. Using the delegateObj in classB we can invoke the protocol methods in classA. i.e classB delegates its job to classB.


@interface classA :NSObject 


#import "classA.h"
#import "classB.h"

@implementation classA
   classB *objB = [[classB alloc] init];
   objB.delegateobj = self;   //setting weak referenc of classA to classB object

    //Delegated Task from classB



@protocol MyProtocol 

@interface classB : NSObject
@property (weak) id delegateObj; //Holds the weak reference to classA object


#import "classB.h"

@implementation classB

 [self.delegateObj myProtocolMethod]; //This call delegates the task to protocol 
 //method in classA


Other Details on Protocols:

Formal Protocols:

A formal protocol is a set of methods that must be implemented in any conforming class. This can be seen as a certification regarding a class, ensuring that it is able to handle everything that is necessary for a given service. A class can conform to an unlimited number of protocols.

Informal Protocols:

The informal protocol is not really a ‘protocol‘ ,instead it creates no constrains upon the code. But it is ‘informal‘ in nature and contributes for code auto documentation. This lets us to group methods by application field, so that it organize its classes consistently. We use the concept called ‘class category‘ to implement informal protocols.

 To Know more refer:  Protocol

KVO In Objective – C — June 17, 2015

KVO In Objective – C

What is KVO ?(Key Value Observing)

KVO – Key Value Observing ,is the feature provided by COCOA which allows an object to register itself to be notified when another object changes the value of one of its properties. Here the controller would ask the model object for notification when the data changes , when the controller receives the notification from the model, the view is updated.

KVO simplify the process of registering for notifications ,and notifying any changes any objects that need to be told of the changes. Any property of an object can be observed , as long as that property’s name is key value coding compliant.

How to Register an Object For Change Notifications?

An object can register itself to be notified of the changes of its properties by the other object, by  telling the object to be observed the following : the object that should be notified when the property changes, the name of the property that should be observed, and the information the observer should be told about when the when change happens. Optionally , we can also include the pointer or object reference that should be passed to the method that is run when the change in the value of the property happens.

Eg: –

[aPerson addObserver:self

In the above snippet we see that the object we add an observer for the ‘personName’ property of the object ‘aPerson’ to be observed by the ‘self’ object , which is notified with the old and new values of the property ‘personName’ of the ‘aPerson’ object.

Once the object has registered itself as the observer of another object, this object recieves the message ‘observeValueForKeyPath:ofObject:change:context:‘. This message has the following parameters:

  • The key path of the property that changed -[personName]
  • The object whose property changed -[aPerson]
  • An NSDictionary that contains information about the change -[change dictionary]
  • The context variable that was passed in when addObserver:forKeyPath:options:context: was called -[nil]

The change dictionary contains the different information dependin on what options wer passed in when the observer was added.


  • NSKeyValueObservingOptionNew :  then the dictionary contains the NSKeyValueChangeNewKey key, whose object is the value that the property has been set to.
  •  NSKeyValueObservingOptionOld : then the dictionary contains the  NSKeyValueChangeOldKey key, which can be used to get the previous value of the property.


The example for how an object can handle the aPerson object changing its personName property :

- (void)observeValueForKeyPath:(NSString *)keyPath
                        change:(NSDictionary *)change
                       context:(void *)context
       if ([keyPath isEqualToString:@"productName"]) {
           NSString* newPersonName = [change objectForKey:NSKeyValueChangeNewKey];
        // tell the appropriate view to update, based on the newPersonName variable.        }

How to Notify the Observers of the Changes?

Whenever implementing the key-value observation, the objects need to notify their observers when their properties change.

If the properties are declared using the ‘@property’ syntax and have compiler synthesize the accessor methods, then the object is automatically notify any registered observers when the setter methods are called.

If Objective-C properties syntax is not used for declaring the properties, or if you override the setter methods for a property , you need to manually notify the system of the changes that are being made.To do this, you call the willChangeValueForKey: and didChangeValueForKey: methods on the self object. This allows the key-value observing system to keep track of the previous and new values of a property.

Eg: – How to override the setter method for the personName property of aPerson object.

- (void) setPersonName:(NSString*)newPersonName {
          [self willChangeValueForKey:@"personName"];
          productName = newProductName;
          [self didChangeValueForKey:@"productName"];

To know more on KVO refer:

  1. Introduction to Key-Value Observing Programming Guide
  2. Understanding Key-Value Observing and Coding
KVC in Objective-C — April 29, 2015

KVC in Objective-C

 What is KVC?

KVC- Key Value Coding in Objective-C provides a mechanism for accessing the object properties indirectly. KVC makes use of strings to identify the objects properties ,rather than using the accessor methods or accessing them directly through instance variables.

KVC is the core technology of the binding technologies used by the controller layer (MVC), also a key mechanism behind the Core Data. By using KVC we can make the application scriptable, also using the KVC methods can simplify the applications code.

What are Keys and Key Paths?

Key is a string that identifies a specific property of  an object. Typically, a key corresponds to the name of an accessor method or instance variable in the receiving object.

Eg: For a "Person Object" the keys would be firstName,lastName,emailId,address,empId. and so on..

Key Path is string of dot separated keys that is used to specify a sequence of object properties to traverse. The property of the first key in the sequence is relative to the receiver, and subsequent key is evaluated relative to the value of the previous property.

Eg: In case of the person object again the key path 'address.street' would get the value of the address property of the receiving 'person' object , and then determine the 'street' property relative to the 'address' object.

How to get attribute values using Key – Value Coding?

We can easily get the attribute values using the method, ‘valueForKey: ‘ which return the value for the specified key, relative to the receiver. If there is no accessor or instance variable for the specified key, then the receiver sends the message valueForUndefinedKey:  to itself . The default implementation of this method raises an exception NSUndefinedKeyException; The sub classes can override this behavior.

Eg: NSString *fName = [person valueForKey:@"firstName"];
Here the string fName holds the value for the property 'firstName' of the object 'person'.

How to set attribute values using Key – Value Coding?

The method setValue:forKey: sets the value of the specified key, relative to the receiver, to the provided value.If the specified key does not exist, the receiver is sent a setValue: forUndefinedKey: message. The default implementation of this method raises an NSUndefinedKeyException ; however,subclasses can override this method to handle the request in a custom manner.

The method setValue:forKeyPath: behaves in a similar fashion, but it is able to handle a key path
as well as a single key.The setValuesForKeysWithDictionary: sets the properties of the receiver with the values in the specified dictionary, using the dictionary keys to identify the properties. The default implementation invokes setValue:forKey: for each key-value pair, substituting nil for NSNull objects as required.

One additional issue that you should consider is what happens when an attempt is made to set a non-object property to a nil value. In this case, the receiver sends itself a setNilValueForKey: message. The default implementation of setNilValueForKey: raises an NSInvalidArgumentException . Your application can override this method to substitute a default value or a marker value, and then invoke setValue:forKey: with the new value

Get Sample XCode Project showing use of KVC Here!

To know more on KVC refer:

1.Key Value Coding Programming Guide by apple.

2.Understanding Key-Value Observing and Coding