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

别来无恙 提交于 2019-11-27 23:01:10

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.

Kendall Helmstetter Gelner

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!

Just use two pairs of parentheses to make it clear to the compiler that you're assigning on purpose:

if ((self = [super init]))

Bring up the project navigator and choose your project. In the main window that appears, choose "All". Under the section "LLVM compiler 2.0 - Warnings", choose "Other Warning Flags". Add the flag "Wno-idiomatic-parentheses" for both "Debug" and "Release." Now clean and recompile.

As a few others have suggested you should add an extra set of parenthesis.

I'm far from a regular expression guru so feel free to clean this up but this find and replace in Xcode fixed about 95% of my instances:

Replace: if\s*\({1}\s*self\s*={1}(.*)\){1}
With:    if ((self =\1))

Be careful because this will also find if (self == ...), so use preview and uncheck those or fix my regex :)

And start using self = ...; if (self), it's cleaner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!