我正在尝试制作一个具有多个值的简单 if-let 语句。if
仅当所有可选 var 都非 nil 时才应执行该块,并且应将它们分配给仅存在于该if
块内的新 let-var(常量?),就像普通的单赋值 if-let 一样。
var a: String? = "A"
var b: String? // nil
if let (m, n) = (a, b) {
println("m: \(m), n: \(n)")
} else {
println("too bad")
}
// error: Bound value in a conditional binding must be of Optional type
// this of course is because the tuple itself is not an Optional
// let's try that to be sure that's the problem...
let mysteryTuple: (String?, String?)? = (a, b)
if let (m, n) = mysteryTuple {
println("m: \(m), n: \(n)")
} else {
println("too bad")
}
// yeah, no errors, but not the behavior I want (printed "m: A, n: nil")
// and in a different way:
if let m = a, n = b {
println("m: \(m), n: \(n)")
} else {
println("too bad")
}
// a couple syntax errors (even though 'let m = a, n = b'
// works on its own, outside the if statement)
这甚至可能吗?如果不是(我猜),您认为 Apple 将来会(或应该)实现这一点吗?