Cocoa Core Competencies (Cocoa的核

2018-02-26  本文已影响23人  struggle3g

非正式协议

    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. 

    非正式协议是NSObject(包括子类)的一个类别,几乎所有的NSObject类及子类都接受这个协议。(一个类别是Objective-C语言特性,它使您可以在不子类化的情况下向类中添加方法。)在非正式协议中实现方法是可选的。在调用方法之前,调用对象检查目标对象是否实现它。在Objective-C 2.0中引入了可选的协议方法之前,非正式的协议是Foundation和AppKit类实现委托的唯一方法。

正式协议

    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.

    一个正式的协议声明了一个客户端类将要实现的方法列表。正式协议有自己的声明、采用和类型检查语法。您可以使用@required和@optional关键字指定实现是必需的或可选的方法。子类继承其祖先所采用的正式协议。正式协议还可以采用其他协议。

正式协议是对Objective-C语言的扩展。


转载:

    苹果官方文档Cocoa Core Competencies

上一篇 下一篇

猜你喜欢

热点阅读