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])
}
}