2
I am trying to pass a dictionary from one Viewcontroller to another, I have succeeded, but the dictionary I am passing through is empty. I want to pass the dictionary after adding the userinput when clicking the save button (createFav)
This is my Viewcontroller where I’m taking user input and inserting it into the dictionary:
var favsDictionary = [String: String]()
var rowsInSection = 1
@IBAction func createFav(sender: AnyObject) {
var userInputToTrimm = userInput.text
var trimmUserInput = userInputToTrimm.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
var zipInStr: String = zipCode.text!
//Este if/else confere se a entrada do usuario não é nulo
if (userInputToTrimm == String()){
println("Please insert a name")
} else if (trimmUserInput == String()) {
println("Please input a valid name")
} else {
favsDictionary[trimmUserInput] = zipInStr
}
}
And this is how I’m importing the favsDict in the other View:
var cellDictionary = ViewController().favsDictionary
I was a little confused with the names you used. Could you refactor the names? For example, where did you get the textInput? what is newTextInput? If you can, read it here: Coding Horror
– Abner Terribili
I’ve improved the variables, I hope it’s clearer.
– Skalwalker
Is this already your whole code? How are you making the transition from the screens? The way you are trying to get the data from
ViewController
distinct is not the way, so I ask about the transition because the correct form is there.– Paulo Rodrigues
Not this is not the whole code, but I just managed to solve the problem, just create the dictionary in a file of Swift that was not a View
– Skalwalker