if (self = [super init]) vs. if ((self = [super init]))(if (self = [super init]) 与 if ((self = [super init])))
问题描述
刚刚进行代码审查并开始怀疑:
Was just doing a code review and started to wonder:
我认为 if (self = [super init])
检查将 [super init]
的返回值分配给变量 self
是否成功与否(操作价值).因此 (self = nil)
实际上是 TRUE
.
I thought if (self = [super init])
checks whether assigning return value of [super init]
to variable self
was successful or not (value of operation). Thus (self = nil)
would actually be TRUE
.
我以为 if ((self = [super init]))
在赋值后检查 self
的值是什么(变量的值).因此 ((self = nil))
将是 FALSE
.
I thought if ((self = [super init]))
checks what is the value of self
after assignment (value of variable). Thus ((self = nil))
would be FALSE
.
在初始化您自己的类时使用哪种方法是正确的?Apple 文档使用前一个(例如这里),我现在实际使用的样式.
Which one is the correct way to use when initialising your own classes? Apple documentation uses the former one (for example here), which style I'm actually using now.
推荐答案
他们都做同样的事情.if
评估的是其中表达式的值,即赋值中的赋值.所以当 self
不是 nil 时,你继续进入 if
块.
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.
第二种形式在它周围抛出括号,以消除任何潜在的编译器关于条件内赋值的警告,这通常是不好的做法,可能是一个错字.但这是惯用的 Objective-C,所以用第一种方式就可以了.
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.
这篇关于if (self = [super init]) 与 if ((self = [super init]))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:if (self = [super init]) 与 if ((self = [super init]))
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01