Xcode does not warn about invalid init methods

I just noticed that if you try to call a nonexistent init method on the same line as the alloc statement, Xcode will not warn you about this.

NSString *string = [[NSString alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; UIView *view = [[UIView alloc] initWithCapacity:0]; 

There are no warnings for any of these lines of code. Now, I think, this is because "alloc" actually returns id, as opposed to a static type. However, when I start typing "init", Xcode gives me autocomplete options that only include init methods for the correct type. So, if he uses my static typing at the beginning of the line to give me autocomplete options, why not use this to warn me? Perhaps this is a parameter somewhere in Xcode that you can enable?

Obviously, this does not matter for obviously wrong things like these examples, but for some objects this can lead to runtime errors due to typos or forget to change one when you change another thing. I could also get a compiler warning by splitting the alloc and init methods on two different lines of code, but I never saw this; I have always learned to use one line of code for alloc and init.

+4
source share
1 answer

First of all, an interesting question!

Xcode uses gcc output to create a list of warnings and errors and some other (internal) mechanism to create a list of autocomplete suggestions. Autocomplete is smarter sometimes (and sometimes not, you know :)

But why gcc does not warn? You already have an answer - alloc returns id .

My answer is not a "response", but I want to share the following with you. (Sorry, I don't have a Mac for a moment, so this is not Apple objective-c)

 shum@shum-laptop :/tmp/shum$ cat test.m #import <objc/Object.h> @interface Test1 : Object { } - (id) blah; @end @interface Test : Object { } @end @implementation Test @end int main() { Test* test = [[Test alloc] blah]; return 0; } shum@shum-laptop :/tmp/shum$ gcc test.m -lobjc shum@shum-laptop :/tmp/shum$ 

There are no warnings. But try to comment on - (id) blah in Test1 interface

 shum@shum-laptop :/tmp/shum$ gcc test.m -lobjc test.m: In function 'main': test.m:24: warning: no '-blah' method found test.m:24: warning: (Messages without a matching method signature test.m:24: warning: will be assumed to return 'id' and accept test.m:24: warning: '...' as arguments.) shum@shum-laptop :/tmp/shum$ 

We can conclude: if you overclock with an init call and there is no selector with the same name, gcc should warn you; in a bad case, when a selector with the same name exists somewhere, gcc will not warn you. I don't have a Mac or Xcode for a moment, could you test it?

[EDIT] Just tested it with xcode. The same behavior.

+3
source

All Articles