Object-C中的非正式协议与正式协议

  之前对 "非正式协议"一词不甚理解,查阅一番后就自己的理解简单梳理一下,

  下面是苹果官方文档Cocoa Core Competencies一文中对非正式协议的介绍:

  An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

  可见非正式协议就是我们常用的类别(Category),而正式协议就是一个以@protocol方式命名的方法列表,与非正式协议相比不同的是,它要求显示的遵守协议。

posted @ 2016-01-04 18:34  moyazi  阅读(349)  评论(0编辑  收藏  举报