0

So the idea is that users can create collections with many pairs of words in it.

For example, they created a collection nouns and added inside some words and the words' translations. And then they created another collection named colors and added corresponding keys and values and so on:

userInput = {
    nouns: {
        "dog": "translation",
        "cat": "translation",
        "fish": "translation"
         //etc
    },
    colors: {
        "red": "translation",
        "green": "translation",
        "blue": "translation"
        //etc
    }
    //etc..
}

How do I implement this in Swift?

Does it mean that I have to create new dictionaries every time the user adds new collections?

I have tried doing something like

var dict = [String: AnyObject]()
var collectionNanme = input.text
dict[collectionNanme] = [Dictionary<String, String>]() as AnyObject

dict[collectionNanme].append([word1.text: word2.text])

But the number of erros in uncountable! What is the proper way to do this?

update:

import UIKit
var main = [String: [String:String]]()

class ViewController: UIViewController {

    @IBOutlet weak var collection: UITextField!
    @IBOutlet weak var input1: UITextField!
    @IBOutlet weak var input2: UITextField!

    var collectionName = collection.text!
    var inner:[String:String] = main[collectionName] ?? [:]

    inner[input1.text!] = input2.text!
    main[collectionName] = inner

    @IBAction func click(_ sender: Any) {
        print(main[collectionName])
    }
}
dwin812
  • 133
  • 10

1 Answers1

2

You need

var main = [String: [String:String]]()

Get current value

var collectionName = input.text! // nouns / colors
var inner:[String:String] = main[collectionName] ?? [:] 

Change and set back

inner["dog"] = "translation"
main[collectionName] = inner

import UIKit

var main = [String: [String:String]]()  // global
class ViewController: UIViewController {

    @IBOutlet weak var collection: UITextField!
    @IBOutlet weak var input1: UITextField!
    @IBOutlet weak var input2: UITextField! 


    var collectionName = "nouns"

    override func viewDidLoad() {
        super.viewDidLoad()

        var collectionName = collection.text! // collection.text! will be "" as textfield is empty when the above function is called 
        var inner:[String:String] = main[collectionName] ?? [:] 
        inner[input1.text!] = input2.text!
        main[collectionName] = inner

    }

    @IBAction func click(_ sender: Any) {
        print(main[collectionName])
    }
}

OR

import UIKit

var main = [String: [String:String]]()  // global
class ViewController: UIViewController {

    @IBOutlet weak var collection: UITextField!
    @IBOutlet weak var input1: UITextField!
    @IBOutlet weak var input2: UITextField! 


    var collectionName = "nouns"

    override func viewDidLoad() {
        super.viewDidLoad()



    }

    @IBAction func save(_ sender: Any) { // save button action
        print("Before save : " , main)

        var collectionName = collection.text!  
        var inner:[String:String] = main[collectionName] ?? [:] 
        inner[input1.text!] = input2.text!
        main[collectionName] = inner

       print("After save : " , main)    
    }

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • wow yes! it actually works but.. I've been trying to put this code in my view controller but with no success so far. can you please check out the update and tell me what I'm doing wrong? – dwin812 Mar 25 '19 at 00:51
  • and how can I make all of this global to access on other view controllers? – dwin812 Mar 25 '19 at 00:56
  • 1
    see edit also you should assign default values for the textfields in IB , or make the code inside `viewDidLoad` to be in an action – Shehata Gamal Mar 25 '19 at 01:00
  • great!! this seems legit.. but did you try to actually run it? it takes forever launching the project and in fact, never even does. I am very confused. What do you think is the problem? – dwin812 Mar 25 '19 at 01:28
  • there was a missed `{` check it again if runs ? – Shehata Gamal Mar 25 '19 at 09:54
  • yes, i noticed, the program gives no error, successfully builds but doesn't launch – dwin812 Mar 25 '19 at 10:14
  • https://stackoverflow.com/q/8244953/10993831 it isn't like in this case.. – dwin812 Mar 25 '19 at 10:16