为什么 GCC -Wselector 和 -Wundeclared-selector 对声明的选择器发出警告?
GCC 的 -Wselector
和 -Wundeclared-selector
选项的描述使它们听起来像是防止 Objective-C 中拼写错误的出色编程工具 @selector()表达式。
然而,在我的代码库上启用它们后,我收到如下警告:
Unimplemented selector 'reloadData'
指向如下行:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
这行代码在我们的代码库中相当常见。数据是异步加载的,一旦加载,上面的代码行将用于通知 UITableView 重新加载(在主线程上,因为从后台线程调用 UIKit 是一个坏主意)。
我的问题是多部分的:
- 上面的代码行在某种程度上是否不正确?是否有更好的模式在主线程上向
UITableView
发送信号以重新加载其数据? - 当
UITableView.h
中明确有reloadData
选择器的声明时,为什么 GCC 会发出警告? - 有没有办法让 GCC 警告未声明的选择器,而不导致它抱怨明确声明的选择器?
The description of GCC’s -Wselector
and -Wundeclared-selector
options make them sound like wonderful programming tools to guard against typos in Objective-C @selector()
expressions.
However, upon enabling them on my codebase, I get warnings like this:
Unimplemented selector 'reloadData'
pointing to lines like this:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
This line of code is fairly common in our codebase. Data is loaded asynchronously, and once it is loaded, the above line of code is used to signal the UITableView
to reload (on the main thread, since calling UIKit from background threads is a bad idea).
My question is multi-part:
- Is the above line of code incorrect in some way? Is there a better pattern for signalling
UITableView
, on the main thread, to reload its data? - Why does GCC warn about this when there is clearly a declaration in
UITableView.h
for thereloadData
selector? - Is there a way to get GCC to warn about undeclared selectors without causing it to complain about selectors which clearly are declared?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我是
-Wundeclared-selector
的粉丝。我还没有看到这个问题出现。您是否在此特定文件(或您的 .pch)中#import
?我无法使用-Wundeclared-selector
在简单程序中重现此问题。tableView
IBOutletviewDidLoad
将
reloadData
更改为reloadDat
我收到警告(如预期):您说您收到“未实现的选择器‘reloadData’”。这真的是警告吗,还是您的意思是输入“未声明”?
Yep, I'm a fan of
-Wundeclared-selector
. I haven't seen this problem crop up. Did you#import <UIKit/UIKit.h>
in this particular file (or in your .pch)? I can't reproduce this problem in a simple program with-Wundeclared-selector
.tableView
IBOutletviewDidLoad
Change
reloadData
toreloadDat
and I get the warning (as expected):You say you're getting "Unimplemented selector 'reloadData'." Is that really the warning, or did you mean to type "Undeclared?"