【问题标题】:Using multiple let-as within a if-statement in Swift在 Swift 的 if 语句中使用多个 let-as
【发布时间】:2014-07-06 01:43:32
【问题描述】:

我从字典中解包两个值,在使用它们之前,我必须转换它们并测试正确的类型。这是我想出的:

var latitude : AnyObject! = imageDictionary["latitude"]
var longitude : AnyObject! = imageDictionary["longitude"]

if let latitudeDouble = latitude as? Double  {
   if let longitudeDouble = longitude as? Double {
       // do stuff here
   }
}

但我想将这两个 if let 查询合并为一个。所以它会是这样的:

if let latitudeDouble = latitude as? Double, longitudeDouble = longitude as? Double {
    // do stuff here
}

那个语法不起作用,所以我想知道是否有一个很好的方法来做到这一点。

【问题讨论】:

标签: if-statement swift optional


【解决方案1】:

Swift 3 更新:

以下内容适用于 Swift 3:

if let latitudeDouble = latitude as? Double, let longitudeDouble = longitude as? Double {
    // latitudeDouble and longitudeDouble are non-optional in here
}

请务必记住,如果尝试的可选绑定之一失败,if-let 块内的代码将不会被执行。

注意:子句不必都是“让”子句,您可以使用逗号分隔的任何一系列布尔检查。

例如:

if let latitudeDouble = latitude as? Double, importantThing == true {
    // latitudeDouble is non-optional in here and importantThing is true
}

Swift 1.2:

Apple 可能已经阅读了您的问题,因为您希望的代码在 Swift 1.2(今天处于测试阶段)中正确编译:

if let latitudeDouble = latitude as? Double, longitudeDouble = longitude as? Double {
    // do stuff here
}

Swift 1.1 及更早版本:

这是个好消息 - 您完全可以做到这一点。两个值的元组上的 switch 语句可以使用模式匹配将它们同时转换为 Double

var latitude: Any! = imageDictionary["latitude"]
var longitude: Any! = imageDictionary["longitude"]

switch (latitude, longitude) {
case let (lat as Double, long as Double):
    println("lat: \(lat), long: \(long)")
default:
    println("Couldn't understand latitude or longitude as Double")
}

更新:这个版本的代码现在可以正常工作了。

【讨论】:

  • 它在 6.1.1 中适用于我,@AaronBratcher 为什么不适用于你?
  • 在 Swift 1.2 中,现在可以在一行中做到这一点:stackoverflow.com/a/28418847/1698576
  • 在您的代码中,您有 2 个选项被解包。总是这样使用吗?我有不同的混乱代码。 if let app = UIApplication.sharedApplication().delegate as? AppDelegate, let window = app.window {...}2nd let 也是可选绑定吗?我的意思是app 不再是可选的。对吗?
  • 是的。 app 不再是可选的,但它的window 属性是(它的类型是UIWindow?),所以这就是你要解包的内容。
【解决方案2】:

使用 Swift 3,您可以使用可选链、switch 语句或可选模式来解决您的问题。


1。使用if let(可选绑定/可选链接)

Swift Programming Language 声明关于可选链接:

多个查询可以链接在一起,如果链中的任何链接为零,则整个链都会优雅地失败。

因此,在最简单的情况下,您可以使用以下模式在可选链接操作中使用多个查询:

let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

if let latitude = latitude as? Double, let longitude = longitude as? Double {
    print(latitude, longitude)
}

// prints: 2.0 10.0

2。在 switch 语句中使用元组和值绑定

作为简单的可选链接的替代方案,switch statement 在与元组和值绑定一起使用时可以提供细粒度的解决方案:

let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

switch (latitude, longitude) {
case let (Optional.some(latitude as Double), Optional.some(longitude as Double)):
    print(latitude, longitude)
default:
    break
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

switch (latitude, longitude) {
case let (latitude as Double, longitude as Double):
    print(latitude, longitude)
default:
    break
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

switch (latitude as? Double, longitude as? Double) {
case let (.some(latitude), .some(longitude)):
    print(latitude, longitude)
default:
    break
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

switch (latitude as? Double, longitude as? Double) {
case let (latitude?, longitude?):
    print(latitude, longitude)
default:
    break
}

// prints: 2.0 10.0

3。使用带有if case 的元组(可选模式)

if case (optional pattern) 提供了一种方便的方法来解开可选枚举的值。您可以将它与元组一起使用,以便通过多个查询执行一些可选链接:

let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

if case let (.some(latitude as Double), .some(longitude as Double)) = (latitude, longitude) {
    print(latitude, longitude)
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

if case let (latitude as Double, longitude as Double) = (latitude, longitude) {
    print(latitude, longitude)
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

if case let (.some(latitude), .some(longitude)) = (latitude as? Double, longitude as? Double) {
    print(latitude, longitude)
}

// prints: 2.0 10.0
let dict = ["latitude": 2.0 as AnyObject?, "longitude": 10.0 as AnyObject?]
let latitude = dict["latitude"]
let longitude = dict["longitude"]

if case let (latitude?, longitude?) = (latitude as? Double, longitude as? Double) {
    print(latitude, longitude)
}

// prints: 2.0 10.0

【讨论】:

    【解决方案3】:

    斯威夫特 3.0

    if let latitudeDouble = latitude as? Double, let longitudeDouble = longitude as? Double {
        // do stuff here
    }
    

    【讨论】:

    • 您应该建议对已接受的答案进行编辑,不要添加另一个质量较低的答案。
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 2018-09-16
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多