【问题标题】:Firebase syntax in Swift 3Swift 3 中的 Firebase 语法
【发布时间】:2016-10-18 06:46:54
【问题描述】:

我已将 Xcode 更新到版本 8,但现在 Firebase 出现了一些问题。这是一段代码:

let target = snapshot.value!["target"] as! NSDictionary
self.myZodiac.text = snapshot.value!["zodiac"] as! String
let nsHeight = snapshot.value!["height"] as! NSNumber

// 类型 'Any' 没有下标成员

在 Swift 2.3 中,所有这些都有效!如何解决?

还有一个:

var messagesDictionary = [[String:Int]]()
userRef.observe(FIRDataEventType.value, with: { (snapshot) in

    for item in snapshot.children.allObjects {
        for itemDic in self.messagesDictionary {
            for (key,value) in itemDic {
                if (item as AnyObject).key == key {
                    var photo = UIImage()
                    let age = (item as AnyObject).value!["age"] as! NSNumber as Int //error Type 'Any' does not conform to protocol 'AnyObject'
                    let name = (item as AnyObject).value!["name"] as! String //error Type 'Any' does not conform to protocol 'AnyObject'
                    if (item as AnyObject).hasChild("avatar"){
                        let avatar = (item as AnyObject).value!["avatar"] as! String //error Type 'Any' does not conform to protocol 'AnyObject'
                        self.storageRef.child(key).child(avatar).data(withMaxSize: 5 * 1024 * 1024, completion: { (data, error) -> Void in
                        if (error != nil) {

                        } else {
                            photo = UIImage(data:data!)!

                        }
                        })
                        ////
                        }else{
                            photo = UIImage(named: "no_avatar")!


                    }

                }

            }
        }
    }


})

我使用的第一个例子:

let target = (snapshot.value as? NSDictionary)?["target"] as! NSDictionary
self.myZodiac.text = (snapshot.value as? NSDictionary)?["zodiac"] as! String
let nsHeight = (snapshot.value as? NSDictionary)?["height"] as! NSNumber

现在如何处理第二段代码中的作为 AnyObject 的项目?

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    FIRDataSnapshot.value 的类型是 Any,所以不能简单地为它下标。

    解决方案是先将值向下转换为字典:

    ref!.observe(.value, with: { (snapshot) in
        for child in snapshot.children {
            let msg = child as! FIRDataSnapshot
            print("\(msg.key): \(msg.value!)")
            let val = msg.value! as! [String:Any]
            print("\(val["name"]!): \(val["message"]!)")
        }
    })
    

    从我这里的回答: Ambiguous Use of Subscript (Swift 3)

    【讨论】:

    • 我看到人们使用child in snapshot.childrenchild in snapshot.children.allObjects - 有什么区别?
    【解决方案2】:

    试试这个:-

    if let snapDict = snapshot.value as? [String:AnyObject]{
    
         let target = snapDict["target"] as! [String:AnyObject]
         self.myZodiac.text = snapDict["zodiac"] as! String
         let nsHeight = snapDict["height"] as! Float
    
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多