create a dictionary with keys and values inside another key on user inputXcode successfully builds my target, but does not run the productHow do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesGetting key with maximum value in dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryCreate a dictionary with list comprehension in PythonGet key by value in dictionaryHow can I sort a dictionary by key?How to remove a key from a Python dictionary?

Does WiFi affect the quality of images downloaded from the internet?

Does the UK delegate some immigration control to the Republic of Ireland?

David slept with Bathsheba because she was pure?? What does that mean?

Was the Lonely Mountain, where Smaug lived, a volcano?

Parsing text written the millitext font

What class is best to play when a level behind the rest of the party?

Is fission/fusion to iron the most efficient way to convert mass to energy?

I am caught when I was about to steal some candies

Harley Davidson clattering noise from engine, backfire and failure to start

Why is my Taiyaki (Cake that looks like a fish) too hard and dry?

Am I allowed to determine tenets of my contract as a warlock?

What is the theme of analysis?

What did the 8086 (and 8088) do upon encountering an illegal instruction?

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

Nth term of Van Eck Sequence

How to import .txt file with missing data?

Are athlete's college degrees discounted by employers and graduate school admissions?

LWC: detect last element in for:each iteration

When editor does not respond to the request for withdrawal

When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?

Is Jesus the last Prophet?

Why would a car salesman tell me not to get my credit pulled again?

Why would a home insurer offer a discount based on credit score?

How do I type a hyphen in iOS 12?



create a dictionary with keys and values inside another key on user input


Xcode successfully builds my target, but does not run the productHow do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesGetting key with maximum value in dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryCreate a dictionary with list comprehension in PythonGet key by value in dictionaryHow can I sort a dictionary by key?How to remove a key from a Python dictionary?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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











share|improve this question






























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











    share|improve this question


























      0












      0








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











      share|improve this question
















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








      swift xcode dictionary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 0:50







      valeria

















      asked Mar 25 at 0:11









      valeriavaleria

      557




      557






















          1 Answer
          1






          active

          oldest

          votes


















          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)








          share|improve this answer

























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

            – valeria
            Mar 25 at 0:51












          • and how can I make all of this global to access on other view controllers?

            – valeria
            Mar 25 at 0: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

            – Sh_Khan
            Mar 25 at 1: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?

            – valeria
            Mar 25 at 1:28












          • does it work on your computer?

            – valeria
            Mar 25 at 9:51











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329805%2fcreate-a-dictionary-with-keys-and-values-inside-another-key-on-user-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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)








          share|improve this answer

























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

            – valeria
            Mar 25 at 0:51












          • and how can I make all of this global to access on other view controllers?

            – valeria
            Mar 25 at 0: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

            – Sh_Khan
            Mar 25 at 1: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?

            – valeria
            Mar 25 at 1:28












          • does it work on your computer?

            – valeria
            Mar 25 at 9:51















          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)








          share|improve this answer

























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

            – valeria
            Mar 25 at 0:51












          • and how can I make all of this global to access on other view controllers?

            – valeria
            Mar 25 at 0: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

            – Sh_Khan
            Mar 25 at 1: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?

            – valeria
            Mar 25 at 1:28












          • does it work on your computer?

            – valeria
            Mar 25 at 9:51













          2












          2








          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)








          share|improve this answer















          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)









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 9:44

























          answered Mar 25 at 0:19









          Sh_KhanSh_Khan

          54k51635




          54k51635












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

            – valeria
            Mar 25 at 0:51












          • and how can I make all of this global to access on other view controllers?

            – valeria
            Mar 25 at 0: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

            – Sh_Khan
            Mar 25 at 1: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?

            – valeria
            Mar 25 at 1:28












          • does it work on your computer?

            – valeria
            Mar 25 at 9:51

















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

            – valeria
            Mar 25 at 0:51












          • and how can I make all of this global to access on other view controllers?

            – valeria
            Mar 25 at 0: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

            – Sh_Khan
            Mar 25 at 1: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?

            – valeria
            Mar 25 at 1:28












          • does it work on your computer?

            – valeria
            Mar 25 at 9:51
















          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?

          – valeria
          Mar 25 at 0:51






          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?

          – valeria
          Mar 25 at 0:51














          and how can I make all of this global to access on other view controllers?

          – valeria
          Mar 25 at 0:56





          and how can I make all of this global to access on other view controllers?

          – valeria
          Mar 25 at 0:56




          1




          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

          – Sh_Khan
          Mar 25 at 1:00






          see edit also you should assign default values for the textfields in IB , or make the code inside viewDidLoad to be in an action

          – Sh_Khan
          Mar 25 at 1: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?

          – valeria
          Mar 25 at 1:28






          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?

          – valeria
          Mar 25 at 1:28














          does it work on your computer?

          – valeria
          Mar 25 at 9:51





          does it work on your computer?

          – valeria
          Mar 25 at 9:51



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329805%2fcreate-a-dictionary-with-keys-and-values-inside-another-key-on-user-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현