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.

  1. @protocol ProtocolName
  2. @required
  3. // list of required methods
  4. @optional
  5. // list of optional methods
  6. @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

  1. @interface MyClass : NSObject <MyProtocol>
  2. ...
  3. @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.

  1. #import <Foundation/Foundation.h>
  2.  
  3. @protocol PrintProtocolDelegate
  4.  
  5. - (void)processCompleted;
  6.  
  7. @end
  8.  
  9. @interface PrintClass :NSObject
  10. {
  11. id delegate;
  12. }
  13.  
  14. - (void) printDetails;
  15. - (void) setDelegate:(id)newDelegate;
  16. @end
  17.  
  18. @implementation PrintClass
  19.  
  20. - (void)printDetails{
  21. NSLog(@"Printing Details");
  22. [delegate processCompleted];
  23. }
  24.  
  25. - (void) setDelegate:(id)newDelegate{
  26. delegate = newDelegate;
  27. }
  28.  
  29. @end
  30.  
  31. @interface SampleClass:NSObject<PrintProtocolDelegate>
  32.  
  33. - (void)startAction;
  34.  
  35. @end
  36.  
  37. @implementation SampleClass
  38.  
  39. - (void)startAction{
  40. PrintClass *printClass = [[PrintClass alloc]init];
  41. [printClass setDelegate:self];
  42. [printClass printDetails];
  43. }
  44.  
  45. -(void)processCompleted{
  46. NSLog(@"Printing Process Completed");
  47. }
  48.  
  49. @end
  50.  
  51. int main(int argc, const char * argv[])
  52. {
  53. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  54. SampleClass *sampleClass = [[SampleClass alloc]init];
  55. [sampleClass startAction];
  56. [pool drain];
  57. return ;
  58. }

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

  1. -- ::50.362 Protocols[:] Printing Details
  2. -- ::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.

Objective-C Protocols的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  3. 【Xamarin笔记】Events, Protocols and Delegates

    Events, Protocols and Delegates   事件.协议和委托 This article presents the key iOS technologies used to re ...

  4. 刨根问底Objective-C Runtime(4)- 成员变量与属性

    http://chun.tips/blog/2014/11/08/bao-gen-wen-di-objective[nil]c-runtime(4)[nil]-cheng-yuan-bian-lian ...

  5. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  6. [Erlang 0112] Elixir Protocols

    Why Elixir   为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由? Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初 ...

  7. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  8. Level Of Management Protocols - SNMP Tutorial

    30.2 The Level Of Management Protocols Originally, many wide area networks included management proto ...

  9. Objective-O Runtime 运行时初体验

    Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一 ...

  10. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

随机推荐

  1. 解压缩zip,tar,tar.gz,tar.bz2文件

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...

  2. linux学习 三 redhat

    1: 查看redhat版本号. 2:   防火墙中加入8080 查看防火墙状态,root用户登录,执行命令systemctl status firewalld 开启防火墙:systemctl star ...

  3. wmware7安装xp错误:虚拟CPU已经入关闭状态

    wm7安装xp错误提示: 虚拟CPU已经入关闭状态.这会造成物理计算机重新启动.这可能是虚拟机操作系统的错误或VMware Workstation软件中的一个配置不正确. 这种情是硬盘模式为AHCI模 ...

  4. GcSpreadSheet自定义Tab键选择

    最开始的时候需要在GcSpreadSheet中按Tab在需要输入的cell中切换,在模板中定义Tab的切换规则:后来又有一个新的要求,因为在使用的时候会出现数据不平的情况,这个时候需要在标记中的不平数 ...

  5. vue中循环时动态绑定值

    在vue项目中,有很多需要动态循环绑定的场景,在未知绑定数量的情况下只能动态生成绑定值 1.首先在data中定义一个对象,我在项目中时循环绑定下拉框,所以定义了一个selectVal = {} 2.在 ...

  6. 201621123016 《Java程序设计》第十四周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 把我原来 ...

  7. css控制页面文字不能被选中user-select:none;

    现象:html中可能有些地方不想让用户复制文字,或是用a标签做了个点击按钮,点快的时候文字会被选中,很丑,这个时候可以使用下面的方案禁止文字选中. 原因:鼠标点快了文字会被选中. 解决方案:不同的浏览 ...

  8. ADT版本查看,This Android SDK requires Andr...ate ADT to the latest问题

    ADT版本查看 Help->About ADT This Android SDK requires Andr...ate ADT to the latest问题 这样的问题很好解决,一个升级AD ...

  9. WPF Set connectionId threw an exception异常 以及重复dll的问题

    1.DataOutputWPF 在显示norlib.Basic.UserConfigControl时 抛出异常 xmlparsingException : WPF Set connectionId t ...

  10. 慕课笔记-Java入门第二季

    1.java对象的使用 (1)创建对象 类名 对象名=new 类名(); (2)使用对象 引用对象的属性:对象名.属性; 引用对象的方法:对象名.方法(); Note: ①Java会给成员变量赋初始值 ...