[英]firebase childByAutoId.key is optional
I m working with Firebase and Swift. 我正在使用Firebase和Swift。 I want to use the Firebase Function childByAutoId.key but it is not working.
我想使用Firebase函数childByAutoId.key,但无法正常工作。 What I m doing wrong?
我做错了什么? The .key is optional... Normally it has to be non optional!
.key是可选的...通常,它必须是非可选的! Thanks for help!
感谢帮助!
import Foundation
import FirebaseDatabase
class AddWatchlistAPI {
var REF_WATCHLIST = Database.database().reference().child("watchlists")
static var shared: AddWatchlistAPI = AddWatchlistAPI()
private init() {
}
//add a watchlist to the user
func addWatchlistToDatabase(watchlistName: String, onSuccess: @escaping () -> Void) {
let watchlistRef = REF_WATCHLIST
let watchlistId = watchlistRef.childByAutoId().key //.key is optional ???? --> Normally it isn t optional
let newWatchlistRef = watchlistRef.child(watchlistId)//here the error is warning
}
}
Try as follows 尝试如下
1- let watchlistId:String = (watchlistRef.childByAutoId().key)!
1-
let watchlistId:String = (watchlistRef.childByAutoId().key)!
or 要么
2- let watchlistId:String = watchlistRef.childByAutoId().key ?? ""
2-
let watchlistId:String = watchlistRef.childByAutoId().key ?? ""
let watchlistId:String = watchlistRef.childByAutoId().key ?? ""
or 要么
3- 3-
var ref: DatabaseReference!
ref = Database.database().reference()
let llave = self.ref.child(name_of_child).childByAutoId().key
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.