ios developer tiny share-20160926

来源:互联网 发布:开发类似唯品会软件 编辑:程序博客网 时间:2024/06/14 04:59

本节开始,我们将讲Objective-C的面向接口编程,先看下本章的OverView。


Working with Protocols

In the real world, people on official business are often required to follow strict procedures when dealing with certain situations. Law enforcement officials, for example, are required to “follow protocol” when making enquiries or collecting evidence.

In the world of object-oriented programming, it’s important to be able to define a set of behavior that is expected of an object in a given situation. As an example, a table view expects to be able to communicate with a data source object in order to find out what it is required to display. This means that the data source must respond to a specific set of messages that the table view might send.

The data source could be an instance of any class, such as a view controller (a subclass of NSViewController on OS X or UIViewController on iOS) or a dedicated data source class that perhaps just inherits from NSObject. In order for the table view to know whether an object is suitable as a data source, it’s important to be able to declare that the object implements the necessary methods.

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface asconforming to a protocol, which means that the class must implement the required methods.

0 0
原创粉丝点击