ios developer tiny share-20160927
来源:互联网 发布:淘宝网商城食品香外香 编辑:程序博客网 时间:2024/06/10 15:51
今天继续讲Objective-C的protocol,讲“Protocols Define Messaging Contracts”。
Protocols Define Messaging Contracts
A class interface declares the methods and properties associated with that class. A protocol, by contrast, is used to declare methods and properties that are independent of any specific class.
The basic syntax to define a protocol looks like this:
@protocol ProtocolName// list of methods and properties@end
Protocols can include declarations for both instance methods and class methods, as well as properties.
As an example, consider a custom view class that is used to display a pie chart, as shown in Figure 5-1.
Figure 5-1 A Custom Pie Chart View
To make the view as reusable as possible, all decisions about the information should be left to another object, a data source. This means that multiple instances of the same view class could display different information just by communicating with different sources.
The minimum information needed by the pie chart view includes the number of segments, the relative size of each segment, and the title of each segment. The pie chart’s data source protocol, therefore, might look like this:
@protocol XYZPieChartViewDataSource- (NSUInteger)numberOfSegments;- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;@end
Note: This protocol uses the NSUInteger value for unsigned integer scalar values. This type is discussed in more detail in the next chapter.
The pie chart view class interface would need a property to keep track of the data source object. This object could be of any class, so the basic property type will be id. The only thing that is known about the object is that it conforms to the relevant protocol.
The syntax to declare the data source property for the view would look like this:
@interface XYZPieChartView : UIView@property (weak) id <XYZPieChartViewDataSource> dataSource;...@end
Objective-C uses angle brackets to indicate conformance to a protocol. This example declares a weak property for a generic object pointer that conforms to the XYZPieChartViewDataSource protocol.
Note: Delegate and data source properties are usually marked as weak for the object graph management reasons described earlier, inAvoid Strong Reference Cycles.
By specifying the required protocol conformance on the property, you’ll get a compiler warning if you attempt to set the property to an object that doesn’t conform to the protocol, even though the basic property class type is generic. It doesn’t matter whether the object is an instance of UIViewController or NSObject. All that matters is that it conforms to the protocol, which means the pie chart view knows it can request the information it needs.
- ios developer tiny share-20160927
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- 轮胎扎了“钉子”该怎么办?到底要不要拔掉?
- C++实现图中的普里姆最短路径算法(P算法)
- WPF编译时出现【错误1程序“C:\...\Debug\MVC_DHCC.exe”不包含适合于入口点的静态“Main”方法】的错误
- JS实现列表的增加和删除功能
- HTML5快速上手之3:与文本格式有关的基本指令
- ios developer tiny share-20160927
- 混合开发的大趋势之一React Native之Image (脑动理解)
- js中的~和取整
- C++ 中不能依赖合成默认构造函数
- JQuery中$.ajax()方法参数详解
- SharePreferences存储数据
- 第五周 项目9 斐波那契数列
- iOS开发技巧:设置状态栏字体颜色
- C# Operators