【发布时间】:2013-07-01 11:08:41
【问题描述】:
如果我在 Xcode 中使用以下代码声明并不完全实现自己的协议:
SomeProtocol.h:
@protocol SomeProtocol <NSObject>
@required
-(void)someRequiredMethod;
@end
SomeImplementor.h:
#import "SomeProtocol.h"
@interface SomeImplementor : NSObject <SomeProtocol>
@end
SomeImplementor.m:
#import "SomeImplementor.h"
@implementation SomeImplementor { // I get a warning on this line
}
@end
然后 Xcode 在 SomeImplementor.h 的 @implementation 行抛出警告,内容如下:
不完整的实现。
协议中的方法“someRequiredMethod”未实现。
但是,如果我使用以下代码未完全实现 UITableView.h 中的UITableViewDataSource 协议...
SomeClass.h:
@interface SomeClass : NSObject <UITableViewDataSource>
@end
SomeClass.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;
为什么?我看不出有什么理由应该区别对待这两种情况。 (我想要我的警告!)
【问题讨论】:
-
@MarkAmery 非常有趣。我看到了相同的结果。
-
@Jai:但请注意,该问题没有可接受的答案,并且该问题增加了更多有价值的信息。所以我会建议 NOT 作为重复关闭。
-
@MarkAmery:我添加了
NSFetchedResultsControllerDelegate:没有警告。 -
@MarkAmery:现在有一些新东西:添加
NSCopying或UIPickerViewDataSource协议会产生警告! - (NSFetchedResultsControllerDelegate 是一个不好的例子,因为所有方法都是可选的。)
标签: ios objective-c xcode cocoa-touch uitableview