I m working with Firebase and Swift. I want to use the Firebase Function childByAutoId.key but it is not working. What I m doing wrong? The .key is optional... Normally it has to be non optional! 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)!
or
2- let watchlistId:String = watchlistRef.childByAutoId().key ?? ""
let watchlistId:String = watchlistRef.childByAutoId().key ?? ""
or
3-
var ref: DatabaseReference!
ref = Database.database().reference()
let llave = self.ref.child(name_of_child).childByAutoId().key
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.