开发者

Making a misnamed selector generate a compile time error

In objective-c how can I make a misnamed selector开发者_运维问答 generate a compile time error?

For example, say I have this

 @selector(some_misnamed_func)

And my class has this member

  -(void)some_func

I want the objective-c compiler to tell me that it can't find that function at compile time rather than generate a run-time exception only when that code is run.


Compile your code with -Wundeclared-selector, or Undeclared selector in Xcode’s build settings. Since that’s a warning only, you can couple it with -Werror (resp. Treat Warnings as Errors in Xcode) to make that warning (and all other warnings) behave as an error and effectively abort the compilation.


The compiler can warn you if there’s no such selector in visible files. And if you turn on treating warnings as errors, you’ll get a nice error. The catch is that if you mistake the right selector for some other existing selector (even on a completely different class), the compiler will not say a thing. This is due to late binding, the compiler can’t know all messages a class will respond to in runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜