How to get rid of the 'undeclared selector' warning

前端 未结 12 2106
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 10:53

I want to use a selector on an NSObject instance without the need for an implemented protocol. For example, there\'s a category method that should set an error prop

相关标签:
12条回答
  • 2020-12-12 11:31

    While the correct answer likely lies in informing Xcode through imports or registering the selector that such a selector exists, in my case I was missing a semi-colon. Make sure before you "fix" the error that perhaps, the error is correct and your code isn't. I found the error in Apple's MVCNetworking sample, for instance.

    0 讨论(0)
  • 2020-12-12 11:32

    Another option would be to disable the warning with:

    #pragma GCC diagnostic ignored "-Wundeclared-selector"
    

    You can place this line in the .m file where the warning occurs.

    Update:

    It works also with LLVM like this:

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"
    
    ... your code here ...
    
    #pragma clang diagnostic pop
    
    0 讨论(0)
  • 2020-12-12 11:33

    I got that message to go away by #include'ing the file with the method. Nothing else was used from that file.

    0 讨论(0)
  • 2020-12-12 11:35

    A really comfortable macro to put in your .pch or Common.h or wherever you want:

    #define SUPPRESS_UNDECLARED_SELECTOR_LEAK_WARNING(code)                        \
    _Pragma("clang diagnostic push")                                        \
    _Pragma("clang diagnostic ignored \"-Wundeclared-selector"\"")     \
    code;                                                                   \
    _Pragma("clang diagnostic pop")                                         \
    

    It's an edit of this question for similar issue...

    0 讨论(0)
  • 2020-12-12 11:41

    I think this is because for some odd reason the selector isn't registered with the runtime.

    Try registering the selector via sel_registerName():

    SEL setErrorSelector = sel_registerName("setError:");
    
    if([self respondsToSelector:setErrorSelector]) {
       [self performSelector:setErrorSelector withObject:[NSError errorWithDomain:@"SomeDomain" code:1 userInfo:nil]];
    }
    
    0 讨论(0)
  • 2020-12-12 11:45

    I realise I'm a bit late to this thread but for completeness, you can globally turn off this warning using the target build settings.

    In section, 'Apple LLVM warnings - Objective-C', change:

    Undeclared Selector - NO
    
    0 讨论(0)
提交回复
热议问题