开发者

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

开发者 https://www.devze.com 2023-02-17 17:51 出处:网络
I have recently (e.g. just now) upgraded to XCode 4, and I like it overal开发者_如何学Gol, however, there is one thing that annoys me.

I have recently (e.g. just now) upgraded to XCode 4, and I like it overal开发者_如何学Gol, 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.


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) {
    ...
}


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.


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.


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.


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


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).


I usually do this.

self = [super init];

if(self) {

}

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


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.....

0

精彩评论

暂无评论...
验证码 换一张
取 消