当我运行程序并更改值并将其发送给Firebase时,出现错误:由于未捕获的异常'InvalidFirebaseData'而终止应用程序,原因:'(updateChildValues:withCompletionBlock :)无法在处存储UITextField类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。
func updateUsersProfile() {
//check to see if the user is logged in
if let userID = Auth.auth().currentUser?.uid {
//create an access point the Firebase storage
let storageItem = storageRef.child("profile_images").child(userID)
//get the image uploaded from photo library
guard let image = profileImageView.image else { return }
if let newImage = image.pngData() {
//upload to Firebase storage
storageItem.putData(newImage, metadata: nil, completion: {
(metadata, error) in
if error != nil {
print(error!)
return
}
storageItem.downloadURL(completion: { (url, error) in
if error != nil {
print(error!)
return
}
if let profilePhotoURL = url?.absoluteString {
guard let newUserName = self.usernameText.text else { return }
guard let newDisplayName = self.displayNameText.text else { return }
guard let newBioText = self.bioText.text else { return }
guard let newDescription = self.descriptionText else { return }
let newValuesForProfile =
["photo": profilePhotoURL,
"username": newUserName,
"displayname": newDisplayName,
"mydescription": newDescription,
"bio": newBioText]
//Update the Firebase database for that user
self.databaseRef.child("profile").child(userID).updateChildValues(newValuesForProfile, withCompletionBlock: { (error, ref) in
if error != nil {
print(error!)
return
}
print("Profile successfully updated")
})
}
})
})
}
}
最佳答案
错误:由于未捕获的异常'InvalidFirebaseData'而终止应用程序,原因:'(updateChildValues:withCompletionBlock :)无法将UITextField类型的对象存储在。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。
错误指出函数updateChildValues:withCompletionBlock
试图存储类型为UITextField
的对象而不是文本。因此,您应该检查是否编写了某些文本字段而不是文本字段的文本。
通过检查,您发现您已将descriptionText
添加到词典而不是文本中。将行更改为:
guard let newDescription = self.descriptionText.text else { return }
关于swift - Xcode Firebase I更新Firebase数据库值时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54722028/