Update for Swift 3:
以下内容将在Swift 3中使用:
if let latitudeDouble = latitude as? Double, let longitudeDouble = longitude as? Double {
// latitudeDouble and longitudeDouble are non-optional in here
}
只要记住,如果try 的可选绑定之一失败,if-let
块中的代码将不会执行.
注意:这些子句不一定都是"let"子句,可以用逗号分隔任何一系列布尔判断.
例如:
if let latitudeDouble = latitude as? Double, importantThing == true {
// latitudeDouble is non-optional in here and importantThing is true
}
Swift 1.2:
苹果可能已经阅读了您的问题,因为您希望的代码在Swift 1.2(今天的测试版)中正确编译:
if let latitudeDouble = latitude as? Double, longitudeDouble = longitude as? Double {
// do stuff here
}
Swift 1.1 and earlier:
好消息是,你完全可以做到这一点.两个值的元组上的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")
}
Update:这个版本的代码现在可以正常工作了.