if (self = [super init]) vs. if ((self = [super init]))

后端 未结 3 427
时光取名叫无心
时光取名叫无心 2020-12-09 08:59

Was just doing a code review and started to wonder:

I thought if (self = [super init]) checks whether assigning return value of [super init]

3条回答
  •  既然无缘
    2020-12-09 09:54

    They both do the same thing. The thing that the if evaluates is the value of the expression inside it, which is the assigned value in an assignment. So when self is not nil, you proceed into the if block.

    The second form throws parens around it to silence any potential compiler warnings about assignments inside conditionals, which is generally bad practice and possibly a typo. But this is idiomatic Objective-C, so it's fine to do it the first way.

提交回复
热议问题