Xcode 4 if (self = [super init]) problem

I recently (for example, now) upgraded to Xcode 4, and I like it in general, but one thing annoys me.

When I write the code as follows:

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

This gives me a "problem": Using the result of an assignment as a condition without parentheses

How can I suppress this warning as it underlines all the text in red, making me think that there is a critical error. Since I'm a somewhat experienced Objective-C encoder, I really don't want to change my methods and add extra parentheses around my init instructions.

+7
source share
8 answers

You can put an extra set of parentheses in the if statement

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

Or you can do what the new templates do.

 self = [super init]; if(self) { ... } 
+11
source

I found the answer to this question here: if (self = [super init]) - LLVM warning! How do you deal with this?

That requires adding the flag "-Wno-idiomatic-brackets" in the building settings. Which helped.

+3
source

You can uncheck the "Missing brackets and brackets" checkboxes in the build settings (in the "GCC 4.2 Warnings" section if you are using GCC4.2 or LLVM GCC4.2).

This is equivalent to the aeprius-related answer that works with LLVM 2.0 but not with GCC 4.2 (verified).

I understand that this warning is now enabled by default to avoid confusion between assignment and testing for equality.

As the Bavarian said here , if (self = [super init]) {...} is idiomatic in Objective-C. The warning has been disabled by default. In Xcode 3.x, and it seems that migrated projects automatically get a "new default"; sorry to get all these warnings about migrated projects.

At the very least, returning a warning will not make the encoding less secure than in Xcode 3.x.

+3
source

Double parentheses.

if ((self = [super init]))

Just make sure you really know what you are doing.

I'm not sure if there is a way to silence the actual warning in XC4, as this is not a compiler warning.

+2
source

change it to if ((self = [super init])) this shows the compiler that it intends.

+1
source

You can put another set of parsens around self = [super init] or you can set self in front of the conditional and then evaluate to (self).

+1
source

I usually do it.

self = [super init];

if (itself) {

}

Thus, nothing and no one will ever be confused.

+1
source

use if (self == [self init]) ..... since you use the assignment operator "=" instead of the condition .... if the instruction checks the condition .... n you are assingng the value there ... use "== "instead of" = "...

Thanx .....

-4
source

All Articles