目前,我在注册过程中结合使用了 Firebase 身份验证、数据库和存储。
@objc func handleSignUp() {
guard let email = emailTextField.text else { return }
guard let password = passwordTextField.text else { return }
guard let fullName = fullNameTextField.text else { return }
guard let username = usernameTextField.text else { return }
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
//handle error
if let error = error {
print("Failed to create user with error", error.localizedDescription)
return
}
//set profile image
guard let profileImage = self.plusPhotoButton.imageView?.image else { return }
//upload data
guard let uploadData = profileImage.jpegData(compressionQuality: 0.3) else { return }
//place image in firbase storage
let filename = NSUUID().uuidString
let storage = Storage.storage()
let storageReference = storage.reference()
let imagesReference = storageReference.child("profile_images")
let uploadReference = imagesReference.child(filename)
uploadReference.putData(uploadData, metadata: nil) { (metadata, error) in
//handle error
if let error = error {
print("Failed to upload image to Firebase Storage with error", error.localizedDescription)
}
//profile image url
let profileImageURL = uploadReference.downloadURL
//user id
let uid = user?.user.uid
let dictionaryValues = ["name": fullName, "username": username, "profileImageURL": profileImageURL]
let values = [uid: dictionaryValues]
//save user info to database
Database.database().reference().child("users").updateChildValues(values) { (error, ref) in
print("Successfully created user and saved information to database")
}
}
}
这应该是代码,应该可以正常工作。 然而,xCode 给了我这个错误:
Heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional
并且建议我改变
let dictionaryValues = ["name": fullName, "username": username, "profileImageURL": profileImageURL]
至
let dictionaryValues = ["name": fullName, "username": username, "profileImageURL": profileImageURL] as [String : Any]
结果,我可以编译并运行该项目。
但是,当我按下注册按钮并运行给定的函数时,控制台显示此错误:
2019-09-30 19:15:13.049986+0200 5iveli0ns[965:28839] [] nw_connection_receive_internal_block_invoke [C2] Receive reply failed with error "Operation canceled"
最后我收到一个运行时异常:
*** Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:withCompletionBlock:) Cannot store object of type __SwiftValue at profileImageURL. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'
这是 xCode 11/Firebase 5.x.x 相关问题吗?
最佳答案
一般来说,字典根据它们包含的数据推断其结构。例如
let dict = [
"name": "Leroy",
"fav_food": "Pizza"
]
是一个[String: String]字典
同样
let dict = [
"name": "Leroy"
"age": 25
]
是一个[String: Int]字典。
你拥有的是
let dict = [
"name": "Leroy",
"username": "some username"
"profileImageUrl": URL //oops, not a string, its a URL!
]
当Dictionary值一致时,可以定义[String: String]。但是,当值类型发生变化时,它们是 [String: Any]
另一个问题是 Firebase 不支持 URL 类型,因此即使您使用 [String: Any] 字典,Firebase 也会提示 - 如错误所示。
修复很简单 - 只需使用路径,它是 url 的字符串
if let url = uploadReference.downloadURL {
let path = url
let dict = [
"name": "Leroy",
"username": "some username",
"path": path //it's a string
]
//write dict to Firebase
}
关于swift - 注册过程中 Firebase 数据无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172529/