Calling dictionary in another View Controller Swift

2

I'm trying to pass a dictionary from one ViewController to another, I succeeded, but the dictionary I'm passing is empty. I want to pass the dictionary after adding the userInput when I click the save (createFav) button

This is my ViewController where I'm picking up the user input and inserting 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 favsDict into the other View:

var cellDictionary = ViewController().favsDictionary
    
asked by anonymous 31.12.2014 / 23:34

1 answer

1

Hello, I saw that you solved the problem and this usually matters, but I can not ignore your case, and how the MVC design pattern would make it easier for you. You could probably have done something without knowing about patterns.

I also used Singleton within the model class because Singleton makes your class use a single instance, so you share information. (There are people who like it and there are people who hate it)

Standards are good to know, have been years of building on programming, but standards are made to facilitate.

I made an example that simulates your situation in PlayGround

import UIKit

class ViewController
{
    init()
    {
        Favoritos.shared.adicionar("fav1", zipCode: "01");
        Favoritos.shared.adicionar("fav2", zipCode: "02");
        Favoritos.shared.adicionar("fav3", zipCode: "03");

        Favoritos.shared.select("fav2");
    }
 }

 class ViewController2
 {

    init()
    {
         println("Selecionou na outra viewcontroller o favorito "+Favoritos.shared.getSelected()!);

        ///aqui vc vai na sua view e muda o texto
    }
  }

 //Nosso modelo
 class Favoritos {

    //padrao singleton
    class var shared: Favoritos 
    {
        struct Static {
            static let instance: Favoritos = Favoritos()
        }
        return Static.instance
    }

    private var dicFavoritos:[String:String] = [String:String]();
    private var selected:String?;

    func adicionar(name:String, zipCode:String)
    {
        //Aqui vc faz toda a logica de validacao como preferir, com ou sem Regexp
        if (name == String()){
            println("Please insert a name")
        } else if (zipCode == String()) {
            println("Please input a valid name")
        } else {
            dicFavoritos[name] = zipCode
        }
    }

   func getFavoritoByName(name:String)->String?
   {
        return dicFavoritos[name]!;
   }

   func select(name:String)
   {
        selected = getFavoritoByName(name)!;
   }

   func getSelected()->String?
   {
        return selected;
   }   
 }


 let viewController = ViewController();
 let viewController2 = ViewController2();

If you want to go deeper, there is a good lesson in English and he talks about MVC very clearly. Free Stanford lessons by Itunes U.

I hope you have contributed

    
05.03.2015 / 15:12