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

前端 未结 8 1004
野的像风
野的像风 2021-01-02 17:36

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:

         


        
相关标签:
8条回答
  • 2021-01-02 17:36

    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.

    0 讨论(0)
  • 2021-01-02 17:36

    I usually do this.

    self = [super init];

    if(self) {

    }

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

    0 讨论(0)
  • 2021-01-02 17:36

    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 讨论(0)
  • 2021-01-02 17:37

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

    0 讨论(0)
  • 2021-01-02 17:42

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

    0 讨论(0)
  • 2021-01-02 17:49

    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.

    0 讨论(0)
提交回复
热议问题