ios developer tiny share-20161008

来源:互联网 发布:如何学数据库管理系统 编辑:程序博客网 时间:2024/05/01 07:29

今天讲Cocoa and Cocoa Touch Define a Large Number of Protocols。


Cocoa and Cocoa Touch Define a Large Number of Protocols

Protocols are used by Cocoa and Cocoa Touch objects for a variety of different situations. For example, the table view classes (NSTableView for OS X and UITableView for iOS) both use a data source object to supply them with the necessary information. Both define their own data source protocol, which is used in much the same way as the XYZPieChartViewDataSource protocol example above. Both table view classes also allow you to set a delegate object, which again must conform to the relevant NSTableViewDelegate or UITableViewDelegate protocol. The delegate is responsible for dealing with user interactions, or customizing the display of certain entries.

Some protocols are used to indicate non-hierarchical similarities between classes. Rather than being linked to specific class requirements, some protocols instead relate to more general Cocoa or Cocoa Touch communication mechanisms that may be adopted by multiple, unrelated classes.

For example, many framework model objects (such as the collection classes like NSArray and NSDictionary) support the NSCoding protocol, which means they can encode and decode their properties for archival or distribution as raw data. NSCoding makes it relatively easy to write entire object graphs to disk, provided every object within the graph adopts the protocol.

A few Objective-C language-level features also rely on protocols. In order to use fast enumeration, for example, a collection must adopt the NSFastEnumeration protocol, as described inFast Enumeration Makes It Easy to Enumerate a Collection. Additionally, some objects can be copied, such as when using a property with a copy attribute as described inCopy Properties Maintain Their Own Copies. Any object you try to copy must adopt the NSCopying protocol, otherwise you’ll get a runtime exception.

0 0