如果我在 Xcode 中使用以下代码声明并不完全实现我自己的协议:
一些协议.h:
@protocol SomeProtocol <NSObject>
@required
-(void)someRequiredMethod;
@end
一些实现者.h:
#import "SomeProtocol.h"
@interface SomeImplementor : NSObject <SomeProtocol>
@end
一些实现者.m:
#import "SomeImplementor.h"
@implementation SomeImplementor { // I get a warning on this line
}
@end
然后 Xcode 会抛出警告@implementation
SomeImplementor.h 行的内容如下:
实施不完整。
协议中的方法“someRequiredMethod”未实现。
但是,如果我不完全执行UITableViewDataSource
来自 UITableView.h 的协议,代码如下...
一些类.h:
@interface SomeClass : NSObject <UITableViewDataSource>
@end
一些类.m:
#import "SomeClass.h"
@implementation SomeClass { // I think I should get a warning here, but I don't
}
@end
...然后 Xcode 就可以了,并且不会在任何地方显示警告,即使我显然还没有实现 UITableViewDataSource 协议中的以下方法:
@protocol UITableViewDataSource<NSObject>
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
为什么?我看不出有任何理由应该对这两种情况进行区别对待。 (我想要我的警告!)