iphone - Objective-C 中的正式协议(protocol)和非正式协议(protocol)有什么区别?

标签 iphone objective-c cocoa cocoa-touch macos

Objective-C 中的正式协议(protocol)和非正式协议(protocol)有什么区别?

最佳答案

来自 Official Documentation

Formal and Informal Protocols

There are two varieties of protocol, formal and informal:

  • 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.

  • A formal protocol declares a list of methods that client classes are expected to implement. Formal protocols have their own declaration, adoption, and type-checking syntax. You can designate methods whose implementation is required or optional with the @required and @optional keywords. Subclasses inherit formal protocols adopted by their ancestors. A formal protocol can also adopt other protocols.

Formal protocols are an extension to the Objective-C language.

关于iphone - Objective-C 中的正式协议(protocol)和非正式协议(protocol)有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082383/

相关文章:

iphone - 添加编译后ITK奇怪的错误

iphone - 将 subview 从一个 View 移动到另一个 View 而不更改 Storyboard中的任何位置信息?

iphone - 核心基础内存泄漏

objective-c - 在拖放过程中在 NSTableView 中打开一个缺口

iphone - 在 UIBarButtonItem 上设置可访问性属性

ios - 如何使用 NSFileManager 保护或加密视频文件?

ios - UITapGestureRecognizer - 检测双击或三次点击?

objective-c - 是否应该在 alloc/new/init 方法上使用 instancetype?

cocoa - 基于 NSDate 类型的属性对自定义对象的 NSArray 进行排序

objective-c - drawRect 真的改变了 NSView 的背景颜色吗?