0

Is there a way to selectively turn off specific errors in Xcode?

Specifically, I want to turn off the following errors

Use of undeclared identifier..

No visible @interface for ... declares the selector

I want this to fail during runtime for certain targets instead at compile time.

jscs
  • 63,694
  • 13
  • 151
  • 195
David
  • 14,205
  • 20
  • 97
  • 144
  • 2
    "Use of undeclared identifier" should ***never*** be turned off, as it indicates an undefined or unknown type or symbol. Besides, even if you could turn it off, the linker would refuse to make you a binary, – CodaFi Apr 04 '13 at 21:55
  • To use unknown selectors (or rather, to forgo the safety of selector checking), use `-[NSObject performSelector:]`, rather than trying to bend the compiler to your will. – CodaFi Apr 04 '13 at 22:01
  • Do have the source? I have a specific use case for this so if it is absolutely not possible then fine, but I don't think you can say it `should never be turned off` – David Apr 04 '13 at 22:02
  • Do I need to have the source to know that it's a bad idea in any language to ignore the compiler? – CodaFi Apr 04 '13 at 22:04
  • @CodaFi I am aware of `-[NSObject performSelector:]`, but it's not exactly what I want. I want to be able to define a class that currently does not exist and have it fail during runtime. This is not just for methods. – David Apr 04 '13 at 22:04
  • That is precisely what the use of `performSelector:` does. If you send any object, even one of type id, a message with `performSelector:`, then you gain the "benefit" of the compiler not warning you about it, and the added dynamism of being able to specify the SEL. The only other possible way would be to know the format of the SEL and drop down to an `objc_msgSend()`. What exactly doesn't work about it? – CodaFi Apr 04 '13 at 22:07
  • `performSelector` is not as elegant as just writing out some arbitrary class. What I'm trying to do is write `BDD` tests like in Ruby using the RSPEC method. – David Apr 04 '13 at 22:09
  • Have you looked at Kiwi? That's exactly what it provides for ObjC. https://github.com/allending/Kiwi (And highly recommended, BTW.) It is a known issue that you must at least create a header file for ARC to be able to compile the tests. Yes, this violates some BDD practice. That is known, but Kiwi does it quite well. – Rob Napier Apr 04 '13 at 22:10
  • `performSelector:` may not be elegant, but it's far more "elegant" than having to write `#pragma clang diagnostic push`, `#pragma clang diagnostic warning ""`, `#pragma clang diagnostic pop` everywhere – CodaFi Apr 04 '13 at 22:11
  • Yeah I'm using Kiwi, but you still have to define the method in the interface file if it does not yet exist. I want to write a test using a method that does not exist and have it fail during runtime without the need to use `performSelector:`. Once the test fails, I will then go and fill in the interface/implementation files that will make the test pass. – David Apr 04 '13 at 22:13
  • I do not believe it can be done much better than Kiwi is currently doing it. – Rob Napier Apr 04 '13 at 22:15
  • @David then how about `objc_msgSend()`, because honestly, you aren't going to find a non-shady way to turn off these warnings. Partially because one of them is a **linker** warning, not a compiler warning, and partially because they are errors, not warnings. They represent a halting of the build mechanism, not just a simple suggestion from LLVM. – CodaFi Apr 04 '13 at 22:15
  • It is possible to write BDD test cases the way you describe if you turn off ARC and let it leak, but I don't think it's worth it. See the beginning of Daniel Steinburg's book for an example of this. https://itunes.apple.com/us/book/test-driving-ios-development/id502345143?mt=11 – Rob Napier Apr 04 '13 at 22:17
  • @CodaFi and Rob, thanks for the help. I will get the book as you suggested. – David Apr 04 '13 at 22:37

1 Answers1

2

As @CodaFi notes, you can't reasonably suppress "use of undeclared identifier." The compiler can't generate code if it doesn't know what the symbol represents.

The normal way to deal with "No visible @interface declares" warning is to just declare the methods. You can do this in an NSObject category like this:

@interface NSObject (AdditionalMethods)
- (void)someUnknownSelector;
@end

This is how we used to create protocols back before you could have @optional members. There's still quite a lot of it scattered around Cocoa.

While it's possible to suppress the warning, that is not recommended since it's confuse ARC. It's better to tell the compiler that you know what you're doing, and give it some hints on what that might be.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610