if nil!=可选…和让_ =可选…之间有什么区别? [英] What's the difference between if nil != optional … and if let _ = optional …
问题描述
我需要测试返回可选值的表达式是否为nil
.这似乎很容易,但这是代码.
I need to test if an expression which returns an optional is nil
. This seems like a no-brainer, but here is the code.
if nil != self?.checklists.itemPassingTest({ $0 === note.object }) {
…
}
出于某种原因,这让我不满意.
Which, for some reason, looks unpleasant to my eye.
if let item = self?.checklists.itemPassingTest({ $0 === note.object }) {
…
}
对我来说看起来好多了,但是我实际上不需要该物品,我只需要知道是否退货即可.因此,我使用了以下内容.
Looks much better to me, but I don't actually need the item, I just need to know if one was returned. So, I used the following.
if let _ = self?.checklists.itemPassingTest({ $0 === note.object }) {
…
}
我在这里错过了一些微妙的地方吗?我认为if nil != optional …
和if let _ = optional …
在这里是等效的.
Am I missing something subtle here? I think if nil != optional …
and if let _ = optional …
are equivalent here.
更新以解决答案中的一些问题
-
虽然我通常使用
var != nil
,但是我看不到nil != var
和var != nil
之间的区别.在这种情况下,将!= nil
推入块后,将得到的块的布尔比较与if的布尔比较混合在一起.
I don't see the difference between
nil != var
andvar != nil
, although I generally usevar != nil
. In this case, pushing the!= nil
after the block gets the boolean compare of block mixed in with the boolean compare of the if.
The use of the Wildcard Pattern should not be all that surprising or uncommon. They are used in tuples (x, _) = (10, 20)
, for-in loops for _ in 1...5
, case statements case (_, 0):
, and more (NOTE: these examples were taken from The Swift Programming Language).
这个问题是关于两种形式的功能等效性,而不是关于编码样式的选择.可以在programmers.stackexchange.com上进行对话.
This question is about the functional equivalency of the two forms, not about coding style choices. That conversation can be had on programmers.stackexchange.com.
所有这些时间之后,Swift 2.0引起了人们的关注
After all this time, Swift 2.0 makes it moot
if self?.checklists.contains({ $0 === note.object }) ?? false {
…
}
推荐答案
优化后,这两种方法可能是相同的.
After optimization, the two approaches are probably the same.
例如,在这种情况下,请使用swiftc -O -emit-assembly if_let.swift
编译以下两项:
For example, in this case, compiling both the following with swiftc -O -emit-assembly if_let.swift
:
import Darwin
// using arc4random ensures -O doesn’t just
// ignore your if statement completely
let i: Int? = arc4random()%2 == 0 ? 2 : nil
if i != nil {
println("set!")
}
vs
import Darwin
let i: Int? = arc4random()%2 == 0 ? 2 : nil
if let _ = i {
println("set!")
}
产生相同的汇编代码:
; call to arc4random
callq _arc4random
; check if LSB == 1
testb $1, %al
; if it is, skip the println
je LBB0_1
movq $0, __Tv6if_let1iGSqSi_(%rip)
movb $1, __Tv6if_let1iGSqSi_+8(%rip)
jmp LBB0_3
LBB0_1:
movq $2, __Tv6if_let1iGSqSi_(%rip)
movb $0, __Tv6if_let1iGSqSi_+8(%rip)
leaq L___unnamed_1(%rip), %rax ; address of "set!" literal
movq %rax, -40(%rbp)
movq $4, -32(%rbp)
movq $0, -24(%rbp)
movq __TMdSS@GOTPCREL(%rip), %rsi
addq $8, %rsi
leaq -40(%rbp), %rdi
; call println
callq __TFSs7printlnU__FQ_T_
LBB0_3:
xorl %eax, %eax
addq $32, %rsp
popq %rbx
popq %r14
popq %rbp
retq
这篇关于if nil!=可选…和让_ =可选…之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!