Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if(self = [super init]) - LLVM warning! How are you dealing with it?

Prior to Xcode 4 with LLVM this passed the compiler unnoticed. Assignment within the conditional is perfectly intentional and a Cocoa idiom.

Xcode 4 with LLVM compiler selected never fails to complain, and not just at compile time, as soon as you type it the yellow warning icon appears. Turning off warnings as errors and just ignoring the warning doesn't seem like a good idea. Moving the assignment out of the parentheses wastes space. Having to turn off this warning with a pragma for every new project will become tedious.

How are you dealing with it? What's the new idiom going to be?

like image 799
Adam Eberbach Avatar asked Feb 09 '11 01:02

Adam Eberbach


2 Answers

This is actually a very old warning, it was just off by default with GCC and with Clang 1.6. Xcode should actually give you a suggestion for how to fix it - namely, double the parentheses.

if ((self = [super init])) { ... }

The extra pair of parens tells the compiler that you really did intend to make an assignment in the conditional.

like image 167
Lily Ballard Avatar answered Oct 09 '22 11:10

Lily Ballard


If you create an init method from the newer Xcode text macros, you'll noticed that the new blessed way to do init is:

- (id)init {
    self = [super init];
    if (self) {
        <#initializations#>
    }
    return self;
}

This avoids the warning. Though personally in my own code if I come across this I've simply been applying the method Kevin showed.

Something good to know!

like image 36
Kendall Helmstetter Gelner Avatar answered Oct 09 '22 12:10

Kendall Helmstetter Gelner