Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.

A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over.

A syntax of protocol is shown below.

@protocol ProtocolName
@required
// list of required methods
@optional
// list of optional methods
@end

The methods under keyword @required must be implemented in the classes that conforms to the protocol and the methods under @optional keyword are optional to implement.

Here is the syntax for class conforming to protocol

@interface MyClass : NSObject <MyProtocol>
...
@end

This means that any instance of MyClass will respond not only to the methods declared specifically in the interface, but that MyClass also provides implementations for the required methods in MyProtocol. There's no need to redeclare the protocol methods in the class interface - the adoption of the protocol is sufficient.

If you need a class to adopt multiple protocols, you can specify them as a comma-separated list. We have a delegate object that holds the reference of the calling object that implements the protocol.

An example is shown below.

#import <Foundation/Foundation.h>

@protocol PrintProtocolDelegate

- (void)processCompleted;

@end

@interface PrintClass :NSObject
{
id delegate;
} - (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end @implementation PrintClass - (void)printDetails{
NSLog(@"Printing Details");
[delegate processCompleted];
} - (void) setDelegate:(id)newDelegate{
delegate = newDelegate;
} @end @interface SampleClass:NSObject<PrintProtocolDelegate> - (void)startAction; @end @implementation SampleClass - (void)startAction{
PrintClass *printClass = [[PrintClass alloc]init];
[printClass setDelegate:self];
[printClass printDetails];
} -(void)processCompleted{
NSLog(@"Printing Process Completed");
} @end int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass startAction];
[pool drain];
return ;
}

Now when we compile and run the program, we will get the following result.

-- ::50.362 Protocols[:] Printing Details
-- ::50.364 Protocols[:] Printing Process Completed

In the above example we have seen how the delgate methods are called and executed. Its starts with startAction, once the process is completed, the delegate method processCompleted is called to intimate the operation is completed.

In any iOS or Mac app, we will never have a program implemented without a delegate. So its important we understand the usage of delegates. Delegates objects should use unsafe_unretained property type to avoid memory leaks.

最新文章

  1. Java dynamical proxy demo
  2. LAMP配置
  3. connect to tomcat with JMX
  4. 《我是一只it小小鸟》
  5. Android如何在ListView中嵌套ListView
  6. Java并发编程专题
  7. Bash判断是否是root
  8. 【Java】数据库连接池技术
  9. ACM1024动态规划
  10. RMQ 与 LCA-ST算法
  11. Tomjson - json 解析库
  12. Android 各层中日志打印功能的应用
  13. SQL Server2008 安装及概述
  14. java集合系列——Map介绍(七)
  15. Windows下MYSQL读取文件为NULL
  16. Android studio,第一个生成,调用成功的jni(说多了都是泪)
  17. oracle12c
  18. MVC接收列表参数
  19. Jade入门学习笔记
  20. CocosCreator项目结构

热门文章

  1. Map容器线程安全问题
  2. 一个轻量级AOP的实现(开源)
  3. 配置tomcat https的步骤
  4. HUD1686(KMP入门题)
  5. python zlib字符串压缩
  6. Thief in a Shop
  7. Ubuntu环境下gedit以及vim的一些个简单配置
  8. 3、css边框以及其他常用样式
  9. Flutter实战视频-移动电商-22.JSON解析和复杂数据模型转换技巧
  10. ASP.NET Core会议管理平台实战_1、开篇介绍