Richard J. Ross III
Richard J. Ross III

Reputation: 55583

XCode 4 if (self = [super init]) issue

I have recently (e.g. just now) upgraded to XCode 4, and I like it overall, however, there is one thing that annoys me.

When I write code like this:

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

It gives me an 'issue': 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. As I am a somewhat seasoned Objective-C coder, I really don't want to change my practices and add extra parentheses around my init statements.

Upvotes: 7

Views: 7421

Answers (8)

user234736
user234736

Reputation:

You can uncheck the 'Missing Braces and Parentheses' in Build settings (under 'GCC 4.2 Warnings' if you use GCC4.2 or LLVM GCC4.2).

This is equivalent to the answer linked by aeprius, which works with LLVM 2.0, but not with GCC 4.2 (tested).

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

As Bavarious noted here, if(self=[super init]){...} is idiomatic in Objective-C. The warning was turned off by default In XCode 3.x and it would appear that migrated projects get the 'new default' automatically; pity to get all these warnings on migrated projects.

At least reverting the warning won't making coding less safe than it used to be in XCode 3.x.

Upvotes: 3

pallavi srivastava
pallavi srivastava

Reputation: 1

use if(self == [self init]).....since you are using a assignment operator " = " in the place of condition .... if statement checks the condition .... n you are assingng a value out there... use "==" instead of "=" ...

thanx.....

Upvotes: -4

aepryus
aepryus

Reputation: 4825

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

Which prescribes adding the "-Wno-idiomatic-parentheses" flag in the building settings. Which did the trick for me.

Upvotes: 3

David
David

Reputation: 14404

I usually do this.

self = [super init];

if(self) {

}

This way, nothing and no one will ever be confused.

Upvotes: 1

Mark Grimes
Mark Grimes

Reputation: 116

You can either put another set of parens around self = [super init] or you can set self before the conditional and then evaluate as if (self).

Upvotes: 1

Grant Limberg
Grant Limberg

Reputation: 21383

You can either put an additional set of parentheses in the if statement

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

Or, you can do as the new templates do.

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

Upvotes: 11

MCannon
MCannon

Reputation: 4041

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

Upvotes: 1

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

Double parenthesize it.

if ((self = [super init]))

It's just making sure you really know what you're doing.

I'm unsure if there is any way to silence the actual warning in XC4, as it isn't a compiler warning.

Upvotes: 2

Related Questions