XCode 4 if (self = [super init]) 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5344810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode 4 if (self = [super init]) issue
提问by Richard J. Ross III
I have recently (e.g. just now) upgraded to XCode 4, and I like it overall, however, there is one thing that annoys me.
我最近(例如刚刚)升级到 XCode 4,总体上我喜欢它,但是,有一件事情让我烦恼。
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
它给了我一个“问题”: 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.
我如何抑制此警告,因为它以红色突出显示所有文本,使我认为存在严重错误。由于我是一名经验丰富的 Objective-C 编码员,我真的不想改变我的做法并在我的 init 语句周围添加额外的括号。
回答by Grant Limberg
You can either put an additional set of parentheses in the if statement
您可以在 if 语句中添加一组额外的括号
if ((self = [super init])) {
...
}
Or, you can do as the new templates do.
或者,您可以像新模板那样做。
self = [super init];
if(self) {
...
}
回答by aepryus
I found the answer to this question here: if(self = [super init]) - LLVM warning! How are you dealing with it?
我在这里找到了这个问题的答案:if(self = [super init]) - LLVM 警告!你是怎么处理的?
Which prescribes adding the "-Wno-idiomatic-parentheses" flag in the building settings. Which did the trick for me.
其中规定在建筑设置中添加“-Wno-idiomatic-parentheses”标志。这对我有用。
回答by aepryus
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).
您可以在构建设置中取消选中“缺少大括号和括号”(如果您使用 GCC4.2 或 LLVM GCC4.2,则在“GCC 4.2 警告”下)。
This is equivalent to the answer linked by aeprius, which works with LLVM 2.0, but not with GCC 4.2 (tested).
这相当于 aeprius 链接的答案,它适用于 LLVM 2.0,但不适用于 GCC 4.2(已测试)。
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.
正如巴伐利亚在这里所指出的, if(self=[super init]){...} 在 Objective-C 中是惯用的。该警告在 XCode 3.x 中默认关闭,并且迁移的项目会自动获得“新默认值”;很遗憾在迁移的项目上收到所有这些警告。
At least reverting the warning won't making coding less safe than it used to be in XCode 3.x.
至少恢复警告不会使编码比以前在 XCode 3.x 中更不安全。
回答by Joshua Weinberg
Double parenthesize it.
用双括号括起来。
if ((self = [super init]))
if ((self = [超级初始化]))
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.
我不确定是否有任何方法可以消除 XC4 中的实际警告,因为它不是编译器警告。
回答by MCannon
change it to if((self = [super init])) this shows the compiler that it is intentional.
将其更改为 if((self = [super init])) 这向编译器表明它是有意的。
回答by Mark Grimes
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).
您可以在 self = [super init] 周围放置另一组括号,也可以在条件之前设置 self ,然后作为 if (self) 进行评估。
回答by David
I usually do this.
我通常这样做。
self = [super init];
self = [超级初始化];
if(self) {
如果(自我){
}
}
This way, nothing and no one will ever be confused.
这样,没有人也不会感到困惑。
回答by pallavi srivastava
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 "=" ...
使用 if(self == [self init]).....因为您使用赋值运算符“=”代替条件.... if 语句检查条件.... n 您正在分配一个值在那里......使用“==”而不是“=”......
thanx.....
谢谢.....