5

如果我在 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 在@implementationSomeImplementor.h 的行上抛出一个警告,内容如下:

实施不彻底。

协议中的方法“someRequiredMethod”未实现。

但是,如果我UITableViewDataSource使用以下代码未完全实现 UITableView.h 中的协议...

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;

为什么?我看不出有什么理由应该区别对待这两种情况。(我想要我的警告!)

4

1 回答 1

3

这可能是 Xcode 4 中的一个错误。

它似乎已在 Xcode 5 中修复,它会适当地警告您。

于 2013-07-03T20:43:37.720 回答