【问题标题】:Why does Xcode 4 not throw a warning over my incomplete implementation of the UITableViewDataSource protocol?为什么 Xcode 4 不会对我的 UITableViewDataSource 协议的不完整实现发出警告?
【发布时间】: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:现在有一些新东西:添加NSCopyingUIPickerViewDataSource 协议会产生警告! - (NSFetchedResultsControllerDelegate 是一个不好的例子,因为所有方法都是可选的。)

标签: ios objective-c xcode cocoa-touch uitableview


【解决方案1】:

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

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

【讨论】:

  • 我希望我有一个带有 iOS 4 SDK 的旧 Xcode 安装。我很确定我在 UITableViewController 子类中看到了有关不完整实现的警告。但那是很久以前的事了,所以我不确定这是否真的发生了,或者是我编造的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2023-03-22
  • 2013-02-05
相关资源
最近更新 更多