用OBJC編程 5 - Working with Protocols
用OBJC編程 5 - Working with Protocols
協議定義了交互的消息
@protocol XYZPieChartViewDataSource
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end
數據源作為View的一個屬性,只要是符合協議的對象就可以,所以類型是id。
@interface XYZPieChartView : UIView
@property (weak) id <XYZPieChartViewDataSource> dataSource;
@end
委托和數據源屬性通常聲明為weak,以避免強引用循環。@property (weak) id <XYZPieChartViewDataSource> dataSource;
@end
設置屬性為一個不符合協議的對象,將會引起一個編譯時警告。
可選方法,使用@optional 和 @required
@protocol XYZPieChartViewDataSource
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
- (BOOL) shouldExplodeSegmentAtIndex:(NSUInteger)segementIndex;
@required
- (UIColor *) colorForSegmentAtIndex:(NSUInteger)segementIndex;
@end
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
- (BOOL) shouldExplodeSegmentAtIndex:(NSUInteger)segementIndex;
@required
- (UIColor *) colorForSegmentAtIndex:(NSUInteger)segementIndex;
@end
運行時檢查可選方法
如果一個方法是可選的,那么在調用前應該檢查它是否實現。
NSString *thisSegmentTitle; // Local object variables are automatically initialized to nil
if([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]){
thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}
if([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]){
thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}
respondsToSelector: 方法用了一個selector,@selector。
如上定義一個符合協議的id類型,調用respondsToSelector,會產生一個編譯時錯誤,解決方案是聲明對象符合NSObject協議
從協議繼承
最佳實踐是,你的協議依從NSObject協議。NSObject對象依從NSObject協議。
一旦你的協議依從NSObject協議,那么依從你協議的任何對象都必須實現NSObject協議的方法,但因為它們應該是NSObject的子類,你就不必自己實現這些NSObject的方法。依從NSObject協議非常有用。
@protocol MyProtocol <NSObject>
@end
@end
comform 一個協議
@interface MyClass : NSObject <MyProtocol, AnotherProtocol>
@end
如果一個類聲明了大量的協議,意味著代碼需要重構成多個小的類。@end
一旦聲明依從某個協議,就必須實現所有的required方法,和需要的optional方法,否則編譯器會給出警告。方法的簽名必須相同。
Cocoa 和 Cocoa Touch 定義了大量的protocol
- view的數據源協議
- view的委托協議delegate
- 一些類似的類,但是無繼承關系,比如NSArray和NSDictionary依從NSCoding協議
- 一些OBJC語言級特性,也依賴協議,如一個容器需要依從NSFastEnumeration協議才能使用快速枚舉fast enumeration;copy的屬性依從NSCopying協議,否則會得到一個運行時異常。
為匿名使用協議
有時候,一個框架的開發者為了向使用者隱藏一個類,只把它的接口通過協議暴露
id <XYZFrameworkUtility> utility = [frameworkObject anonymousUtility];
例如NSFetcheResultsController
NSInteger sectionNumber = //,,,
id <NSFetchedResultSectionInfo> sectionInfo =
[self.fetchedResultsController.sections objectAtIndex:sectionNumber];
NSInteger numberOfRowsInSection = [sectionInfo numberOfObjects];
id <NSFetchedResultSectionInfo> sectionInfo =
[self.fetchedResultsController.sections objectAtIndex:sectionNumber];
NSInteger numberOfRowsInSection = [sectionInfo numberOfObjects];
posted on 2013-11-21 09:32 cuigang 閱讀(331) 評論(0) 編輯 收藏 引用 所屬分類: OBJC