【发布时间】: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