简体   繁体   English

if(self = [super init]) - LLVM警告! 你是怎么处理它的?

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

Prior to Xcode 4 with LLVM this passed the compiler unnoticed. 在使用LLVM的Xcode 4之前,这没有引起注意。 Assignment within the conditional is perfectly intentional and a Cocoa idiom. 条件中的赋值是完全有意的,也是一个Cocoa成语。

Xcode 4 with LLVM compiler selected never fails to complain, and not just at compile time, as soon as you type it the yellow warning icon appears. 选择了LLVM编译器的Xcode 4永远不会抱怨,而不仅仅是在编译时,只要你输入它就会出现黄色警告图标。 Turning off warnings as errors and just ignoring the warning doesn't seem like a good idea. 将警告视为错误而忽略警告似乎不是一个好主意。 Moving the assignment out of the parentheses wastes space. 将分配移出括号会浪费空间。 Having to turn off this warning with a pragma for every new project will become tedious. 必须为每个新项目用pragma关闭此警告将变得乏味。

How are you dealing with it? 你是怎么处理它的? What's the new idiom going to be? 新的成语是什么?

This is actually a very old warning, it was just off by default with GCC and with Clang 1.6. 这实际上是一个非常古老的警告,它默认关闭GCC和Clang 1.6。 Xcode should actually give you a suggestion for how to fix it - namely, double the parentheses. Xcode实际上应该给你一个如何解决它的建议 - 即括号加倍。

if ((self = [super init])) { ... }

The extra pair of parens tells the compiler that you really did intend to make an assignment in the conditional. 额外的一对parens告诉编译器你确实打算在条件中进行赋值。

If you create an init method from the newer Xcode text macros, you'll noticed that the new blessed way to do init is: 如果您从较新的Xcode文本宏创建一个init方法,您会注意到执行init的新祝福方式是:

- (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! 不错的东西!

只需使用两对括号就可以清楚地告诉编译器您是故意分配的:

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". 在“LLVM编译器2.0 - 警告”部分下,选择“其他警告标志”。 Add the flag "Wno-idiomatic-parentheses" for both "Debug" and "Release." 为“Debug”和“Release”添加标记“Wno-idiomatic-parentheses”。 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: 我远离正规表达大师,所以请随意清理它,但是在Xcode中查找并​​替换了大约95%的实例:

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 :) 要小心,因为这也会找到if(self == ...),所以使用预览并取消选中那些或修复我的正则表达式:)

And start using self = ...; 并开始使用self = ...; if (self), it's cleaner. 如果(自我),它更清洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 if(self = [super init])与if((self = [super init])) - if (self = [super init]) vs. if ((self = [super init])) iPhone - 当[super init]失败时使用self = [super init] - iPhone - Use of self = [super init] when [super init] fails 谁能给我一个关于“ self = super init”的简单解释? - Can anyone give me an easy explanation about “self=super init”? 当我初始化自定义单元格时,它没有设置为&#39;[(super或self)init ...]&#39;的结果时返回&#39;self&#39; - Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell 在我的自定义alloc方法中未将其设置为&#39;[((super或self)init…])的结果时返回&#39;self&#39; - Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method “self”时使用的实例变量未通过Analyzer设置为“[(super或self)init ...]”的结果 - Instance variable used while 'self' is not set to the result of '[(super or self) init…]' via Analyzer InitWithCoder,[super init]还是[super initWithCoder]? - InitWithCoder, [super init] or [super initWithCoder]? 自我与超级之间的区别 - Difference between self and super Objective-C中的“超级”是什么? (自我!=超级)? - What is “super” in Objective-C? ( self != super )? 何时在Objective-C的初始化方法中使用self.property,如何在初始化方法中初始化数组 - when to use self.property in init methods in objective-c, how to initialize arrays in init methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM