Reputation: 143
I have created plants entity in firebase db, I want to add another value of "plantId" for some functionality. For this I am creating an random string id and passing it to plant object to be saved in db, but I am getting an error 'Ambiguous use of 'text'. I searched a lot but can't find any solution. please check my code below:
let plantId = NSUUID().uuidString
let plant = ["plantName": self.plantNameField.text, "plantType": self.plantTypeField.text, "plantLocation": self.plantLocationField.text,"userId": userID, "plantImageURL": plantImageURL, "plantId": plantId]
let childUpdates = ["/plants/\(key)": plant]
self.ref.updateChildValues(childUpdates)
Error occurring in above code line number 2
Upvotes: 0
Views: 3108
Reputation: 535880
You have not given enough information, but let's assume your code looks like this. You have an outlet:
@IBOutlet var plantNameField : UITextField!
And then you say, for example:
let plantId = NSUUID().uuidString
let plant = ["plantName": self.plantNameField.text, "plantId":plantId]
That is illegal because self.plantNameField.text
and plantId
have different types, whereas a dictionary in Swift must have values of the same type. To fix it, you would say:
let plant = ["plantName": self.plantNameField.text!, "plantId":plantId]
(Note the exclamation mark.)
And similarly for your other text
entries in the dictionary.
But of course this answer is contingent on the guess that plantNameField
is indeed a UITextField; you have not shown enough code for me to know whether that is true.
Upvotes: 6
Reputation: 117
I think you are missing the creation of the "key"
let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
"author": username,
"title": title,
"body": body]
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)
Maybe you already did but take a look at this. https://firebase.google.com/docs/database/ios/read-and-write The "Key" creation is the only part that is missing.
Upvotes: 1