【问题标题】:Ambiguous use of subscript in Firebase call在 Firebase 调用中不明确地使用下标
【发布时间】:2015-11-25 22:55:13
【问题描述】:

我正在尝试进行 firebase 读取,我正在尝试从 firebase 读取整数,但由于某种原因这不起作用:

func firebaseCall () {

        ref.observeEventType(.Value, withBlock: { snapshot in
            print(snapshot.value)
            let people = snapshot.value["people"] as! Int
            print("People is \(people)")
                       //            let time = snapshot.value["time"] as! Int
            let date = NSDate()
            let calendar = NSCalendar.currentCalendar()
            let components = calendar.components(.Hour, fromDate: date)
            let hour = components.hour

            print("time is \(hour)")



            }, withCancelBlock: { error in
                print(error.description)
        })
    }

我收到错误在 snapshot.value["people"] 上使用下标不明确。我在另一个项目中使用了确切的代码,它可以工作,所以我非常困惑。不同之处在于此代码用于 SKScene 中,而其他时间它用于视图控制器中。任何人都可以帮助解决这个问题吗?或者建议我可以从firebase读取int值的另一种方式?谢谢

【问题讨论】:

  • snapshot是什么类型?
  • 添加print(snapshot.value)的输出。
  • 打印火基调用 is () { frame = "";人= 11;时间=11;花费时间 = 11;框架实际上对我来说太长了(超过 10k 个字符),所以我删除了它,只有“”作为框架
  • 这是我从 firebase 得到的回复

标签: swift firebase skscene


【解决方案1】:

编译器不知道返回的是什么

let people = snapshot.value["people"] as! Int

你可以考虑

if let someInt = snapshot.value.objectForKey("people")
or
if let someInt = snapshot.value as? Int

不知道快照中的内容会产生一些歧义。

如果对象可能为零,您可能需要添加一些方法来处理它

    if let thing = snapshot.value {
        print("thing was not nil")
    } else {
        print("thing was nil")
    }

【讨论】:

  • (另外,如果是 Xcode 6.x,这是一个已知的错误,它可能会在 swift 2 中出现 rdar://22852669)
猜你喜欢
  • 1970-01-01
  • 2016-05-18
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多