swift: Add multiple <key, value> objects to NSDictionary

81,599

You can definitely make a dictionary of dictionaries. However, you need a different syntax for that:

var myDictOfDict:NSDictionary = [
    "a" : ["fname": "abc", "lname": "def"]
,   "b" : ["fname": "ghi", "lname": "jkl"]
,   ... : ...
]

What you have looks like an array of dictionaries, though:

var myArrayOfDict: NSArray = [
    ["fname": "abc", "lname": "def"]
,   ["fname": "ghi", "lname": "jkl"]
,   ...
]

To get JSON that looks like this

{"Data": [{"User": myDict1}, {"User": myDict1},...]}

you need to add the above array to a dictionary, like this:

var myDict:NSDictionary = ["Data" : myArrayOfDict]
Share:
81,599

Related videos on Youtube

Srujan Simha
Author by

Srujan Simha

Updated on April 06, 2020

Comments

  • Srujan Simha
    Srujan Simha about 4 years

    I'm trying to add multiple objects to NSDictionary, like

    var myDict: NSDictionary = [["fname": "abc", "lname": "def"], ["fname": "ghi", "lname": "jkl"], ...]
    

    Is it even possible to do this? If not, please suggest a better way. I actually need to convert this NSDictionary to JSON string and send that to the server, so I need multiple objects in NSDictionary.

    • Antonio
      Antonio over 9 years
      Have you tried it? however that looks like an array of dictionaries
    • Fogmeister
      Fogmeister over 9 years
      Well, your root object there is an array not a dictionary so there's that...
  • Srujan Simha
    Srujan Simha over 9 years
    If I use array of dictionaries then is it possible to convert that to JSON string? And pull out the data at server side?
  • Sergey Kalinichenko
    Sergey Kalinichenko over 9 years
    @SrujanSimha I am pretty certain that the conversion to JSON should go without a problem for any combination of standard collection types. As far as the server side is concerned, you need to look at the format that the server supports. To test this, convert your array of dictionaries to JSON string, and write in the log. Check if the string looks like what your server expects, and adjust it if necessary.
  • Srujan Simha
    Srujan Simha over 9 years
    Ok. Let's say I have a dictionary myDict1 = ["name": "abc", "status": 1] and there are multiple persons, I created myDict1 for each person and added that to another dictionary like .. myDict2 = ["User": myDict1]. So can I have multiple values for same key? Just like JSON: {"Data": [{"User": myDict1}, {"User": myDict1},...]}
  • Sergey Kalinichenko
    Sergey Kalinichenko over 9 years
    @SrujanSimha You cannot have multiple items for the same key. Your example shows a single key with an array in it. Please see the edit for an example.
  • pr1001
    pr1001 over 8 years
    Is it necessary to specify the variables' types as NSDictionary and NSArray? If not, you're throwing out a bunch of Swift type information...
  • Sergey Kalinichenko
    Sergey Kalinichenko over 8 years
    @pr1001 Yes, I think it is necessary for the purposes for which OP is using this code. Of course if you do not insist on getting NSDictionary, you could let Swift use its built-in type.
  • ravi2432
    ravi2432 about 5 years
    is it possible to send multiple array without key index var myDict:NSDictionary = ["Data" : myArrayOfDict] to var myDict:NSDictionary = [ myArrayOfDict ] ?