Swift 无法强制转换类型为';FIRDatabaseReference';(0x108f4d170)至';NSString';(0x10A4 F24A8)
我有一个数据库引用,我想将其更改为字符串,然后再更改为URL类型Swift 无法强制转换类型为';FIRDatabaseReference';(0x108f4d170)至';NSString';(0x10A4 F24A8),swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我有一个数据库引用,我想将其更改为字符串,然后再更改为URL类型 let database = Database.database().reference().child("users/profile/\(uid!)/pathToImage") let string = database as! String // **Code Crashes here let url = URL (string: string)! func setImage() { ImageService.dow
let database = Database.database().reference().child("users/profile/\(uid!)/pathToImage")
let string = database as! String // **Code Crashes here
let url = URL (string: string)!
func setImage() {
ImageService.downloadImage(withURL: url) { image in
self.currentProfileImage.image = image
}
}
setImage()
这个
它不是一种直接的url字符串,它是对图像的引用,您需要单独聆听它
database.observeSingleEvent(of: .value, with: { (snapshot) in
let str = snapshot.value as! String
print(str)
})
这个
它不是一种直接的url字符串,它是对图像的引用,您需要单独聆听它
database.observeSingleEvent(of: .value, with: { (snapshot) in
let str = snapshot.value as! String
print(str)
})
听起来您可能对实时数据库SDK的工作方式感到困惑。如果您有一个数据库引用,并且希望得到该数据库位置的值,那么简单地将该引用转换为字符串是行不通的。您必须在该位置查询数据库,使用回调来接收数据,然后从传递给回调的快照中获取字符串 我建议阅读文档,了解如何执行数据库查询,以了解它是如何工作的。特别是,请注意名为的部分。您的代码看起来更像这样:
let ref = ref.child("path/to/string")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? String
// use the value here
}) { (error) in
print(error.localizedDescription)
}
听起来您可能对实时数据库SDK的工作方式感到困惑。如果您有一个数据库引用,并且希望得到该数据库位置的值,那么简单地将该引用转换为字符串是行不通的。您必须在该位置查询数据库,使用回调来接收数据,然后从传递给回调的快照中获取字符串 我建议阅读文档,了解如何执行数据库查询,以了解它是如何工作的。特别是,请注意名为的部分。您的代码看起来更像这样:
let ref = ref.child("path/to/string")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? String
// use the value here
}) { (error) in
print(error.localizedDescription)
}