【问题标题】:Converting String to Double Value From Firebase从 Firebase 将字符串转换为双精度值
【发布时间】:2017-11-08 03:29:28
【问题描述】:

我正在尝试将我的字符串转换为双精度值,以便可以将其用作 CLLocation 坐标。我在使用两个 let 语句 userLongitude 和 userLatitude 时遇到问题。我收到错误消息:

“类型 'Any' 没有下标成员”。

我是编程新手,其他答案似乎都不起作用。

if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String,
    let userLongitude = (snapshot.value!["long"] as String)?.doubleValue, 
    let userLatitude = (snapshot.value!["lat"] as String)?.doubleValue

【问题讨论】:

  • 您遗漏了重要的代码。 Edit 你的问题是声明和初始化 value 的代码。
  • 请显示您的 Firebase 数据库的结构

标签: swift firebase location


【解决方案1】:

您必须将类型 Any 转换为 Swift 字典类型 [String: Any]

if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String,
    let snap = snapshot.value as? [String: Any],
    let userLongitude = Double(snap["long"] as! String),
    let userLatitude = Double(snap["lat"] as! String)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多