I have a Firebase instance where I want to store a dictionary of values I want to store into firebase. I have looked on the documentation https://www.firebase.com/docs/ios/guide/saving-data.html as a reference but can't seem to get it to work. The following is my attempt:
//Declared above are the currentUser values as so:
var currentUserFirstName: String!
var currentUserLastName: String!
var currentUserObjectID: String!
var attendeesArray = ["objectID": currentUserObjectID, "name": currentUserFirstName + " " + currentUserLastName]
var eventRefChild = EventReference.childByAutoId()
eventRefChild.setValue([
"eventName":eventName.text,
"attendees": attendeesArray,
"eventCreator": currentUserFirstName
])
But I keep getting an error saying: Could not find an overload for '+' that accepts the supplied arguments
when I try to do eventRefChild.setValue([...
and I'm honestly not too sure why I am getting this issue. Any help would be appreciated!
EDIT: The variable EventReference
is assigned as so: EventReference = Firebase(url:"<Insert Firebase URL>")
And inside currentUserFirstName
and currentUserLastName
are an individual's first and last name grabbed from Facebook
so it would look something like Bob
Smith
respectively.
There is nothing wrong with your code. The issue is the values that are being loaded into
var currentUserFirstName: String!
var currentUserLastName: String!
As a test, I created a sample project with the following code, which is a duplicate of your posted code but with normal strings loaded into the var's:
var myRootRef = Firebase(url:"https://myproject.firebaseIO.com/")
var currentUserFirstName = "Test"
var currentUserLastName = "User"
var currentUserObjectID = "object ID"
var attendeesArray = ["objectID": currentUserObjectID, "name": currentUserFirstName + " " + currentUserLastName]
var eventRefChild = myRootRef.childByAutoId()
eventRefChild.setValue([
"eventName": "eventName",
"attendees": attendeesArray
])
the project compiled and runs correctly and the expected data is written to Firebase. Note that the eventName.text was replaced with a string as well but that doesn't affect the answer.
The investigation needs to turn to what's loaded in the var's, and the answer is that one of those var's, currentUserFirstName or currentUserLastName is being loaded with an OBJECT (class), not a string.
As a side note, why are the var's declared as implicitly unwrapped optionals (the !)
edit: adding additional info to deal with the optionals
if let actualString = currentUserFirstName {
println(actualString) //proceed working with the the optional string
}
else {
return // something bad happened! currentUserFirstName does not contain a string
}
To prevent code from errors when an optional contains no value, add the above code directly above the concatenation line of code. What happens here is the we are assigning the string in currentUserFirstName (optional var) to a actual string (a standard, non-optional var).
If the expression evaluates to true then we can proceed evaluating currentUserFirstName.
If it's false, then currentUserFirstName does not contain a string so handle the error elegantly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With