Storing dictionaries in Firebase?

I have a Firebase instance where I want to store a dictionary of values โ€‹โ€‹that I want to store in firebase. I looked at the documentation https://www.firebase.com/docs/ios/guide/saving-data.html as a link, but cannot make it work. Here 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 the error message: 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 get this problem. Any help would be appreciated!

EDIT: EventReference variable EventReference assigned like this: EventReference = Firebase(url:"<Insert Firebase URL>")

And inside currentUserFirstName and currentUserLastName it is an individual first name and last name taken from Facebook , so it will look something like Bob Smith respectively.

+6
source share
1 answer

There is nothing wrong with the code. The problem is the values โ€‹โ€‹that are loaded in

 var currentUserFirstName: String! var currentUserLastName: String! 

As a test, I created a sample project with the following code, which is a duplicate of your published code, but with normal lines loaded in var:

  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 is compiled and working correctly, and the expected data is written to Firebase. Note that eventName.text has also been replaced with a string, but this does not affect the response.

The study should go to what is loaded in var, and the answer is that one of these var variables, currentUserFirstName or currentUserLastName is loaded by an OBJECT (class), not a string.

As a side note, why var is declared as implicitly expanded options (!!)

edit: add additional information for working with options

 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 exclude code from errors, if the option does not contain a value, add the code above directly above the code concatenation line. What happens here, we assign a string in currentUserFirstName (optional var) to the actual string (standard, optional var).

If the expression is true, we can continue to evaluate currentUserFirstName.

If this is false, then currentUserFirstName does not contain a string, so handle the error elegantly.

+7
source

Source: https://habr.com/ru/post/987153/


All Articles