in Education by
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]() 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]) } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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) } }

Related questions

0 votes
    I'm trying to create a region in order to tell my Location Manager which beacon I care about. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Without changing anything in the view controller or updating to a new Xcode Version, displaying a UIAlertController ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    In a Key-Value datastore, both keys and values need to be unique. (1)True (2)False...
asked Apr 23, 2021 in Technology by JackTerrance
0 votes
    I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': ' ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': ' ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have an XML file which is structured as follows: place1 location1 place2 location2 place3 location3 These ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I add a key in python directory after it's creation ? I already tried .add() method but it doesn't work. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How will you get all the keys from the dictionary?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    I have col1 in a pandas df. I want to make col2: col1 col2 1 1 1 2 1 3 1 4 2 2 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    The Function Key F5 is used in a webpage to___________the page (a) Exit (b) Create another window (c) Reload (d) Add a new tab Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I made a function which will look up ages in a Dictionary and show the matching name: dictionary = {'george' ... it search backward. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5}? ... use the "sorted" operator that returns tuples. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...