Home / iPhone

iPhone Developement – Objective C : Protocols

Rating: 4.0Blog-star
Views: 2033
by Ravindra Savaram
Last modified: February 23rd 2021

iPhone Development Protocols

A Protocol is a group of related properties and methods that can be implemented by any class.

Protocols are nothing but interfaces in another language. The Protocol has only method declaration. There are optional methods and required methods. If your class adopts the protocol, then you have to implement those methods in your class.

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

Apple provided mainly pre-defined protocol for the UIKIT  framework, and we can also create over own protocols.

Steps to Create a Protocol:

Right-click ? objective c protocols

Right-click on the classes  ? add


                                                   New file


                                                   Cocava touch classes


                                                   Objective c protocols


                                                      Click on next


                                                    Give name to the protocol




 # import 
   @protocol sample protocol;
  @ optional
   -(void) addition;        // any method declaration;
   -(void) subtraction;
@ end

Implementation of Protocols in a Class:-

Like class interfaces, protocols typically reside in a .h file.

. h file:

# import 
# import “sample protocol’s . h”
#import    “second protocol’ . h”
@ interface protocol view controller: UI view controller
{ } @end
. m file:-

  # import “protocol view controller’s .h”
@ implementation protocol view controller
 -(void) subtraction
-(void) addition

Note: – No need to declare a protocol method in .h file.

Selector: –

A selector is the name of a method. A selector, by itself, doesn’t do anything. It simply identifies a method.

In OBJECTIVE C, selectors are used to referring the name of a method. The keyword for selector I “@selector”

 Ex:-  @ selector (method name)
Passing method as argument

      [Self addition :@selector (subtraction)];
Checking whether method is present or not in the other class,

B*b  = [[ B alloc]init];
If ([b respond selector: @selector (subtraction)]]
     [b subtraction];
       NsLog( “@” no subtraction method in the B class);


@ selector is a unique identity to recognize the method name.


Frequently Asked iPhone Interview Questions & Answers


About Author

NameRavindra Savaram
Author Bio


Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.