【问题标题】:if no value return in set double to a set number with a guard let statement如果在 set double 中没有值,则返回带有 guard let 语句的集合号
【发布时间】:2017-10-12 11:31:10
【问题描述】:

在下面的代码中,它在 else 语句从未使用过之后给出经度和纬度警告,如果我的字典不包含经度和纬度值,它们将被设置为 let longitude = Double(151.209900) 并让纬度 = 双(-33.865143)?还是我的守卫让声明有问题

guard let dictionary = snapshot.value as? [String : AnyObject] else { return }
guard let latitude = dictionary["Latitude"] as? String else {
    let latitude = Double(-33.865143)            
    return 
}
guard let longitude = dictionary["Longitude"] as? String else {
    let longitude = Double(151.209900)
    return 
}       
guard let latDouble = Double(latitude) else { return }
guard let longDouble = Double(longitude) else { return }

【问题讨论】:

  • 在你的函数中设置一个断点,并观察如果字典没有“纬度”键的条目会发生什么。 – 这是你的意图吗?

标签: swift xcode


【解决方案1】:

永远不会使用为纬度和经度设置的值,因为它们的范围仅限于guard

我可能会这样做:

guard let dictionary = snapshot.value as? [String : AnyObject] else { return }


let latitude = Double(dictionary["Latitude"] as? String ?? "-33.865143")
let longitude = Double(dictionary["Longitude"] as? String ?? "151.209900")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多