Calling dictionary in another View Controller Swift

Asked

Viewed 549 times

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

  • I’ve improved the variables, I hope it’s clearer.

  • 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.

  • 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

1 answer

1

Hello, I saw that you solved the problem and that usually matters, but I can’t ignore your case, and how the MVC design standard would make it easier for you. You probably might 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. (Some people like it and some people hate it)

Standards are good to know, were years of construction in programming, but patterns are made to facilitate.

I made an example that simulates your situation in the 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. It’s free Stanford lessons by Itunes U.

I hope you contributed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.