27

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.

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59

3 Answers3

52

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]
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • If I use array of dictionaries then is it possible to convert that to JSON string? And pull out the data at server side? – Srujan Simha Nov 20 '14 at 17:46
  • @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. – Sergey Kalinichenko Nov 20 '14 at 17:50
  • 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},...]}` – Srujan Simha Nov 20 '14 at 19:06
  • 1
    @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. – Sergey Kalinichenko Nov 20 '14 at 19:08
  • Is it necessary to specify the variables' types as `NSDictionary` and `NSArray`? If not, you're throwing out a bunch of Swift type information... – pr1001 Aug 18 '15 at 18:42
  • @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. – Sergey Kalinichenko Aug 18 '15 at 18:46
  • is it possible to send multiple array without key index var myDict:NSDictionary = ["Data" : myArrayOfDict] to var myDict:NSDictionary = [ myArrayOfDict ] ? – ravi2432 May 14 '19 at 10:34
0

SWIFT 3.0

  • List item

Frist of all you can create NSArray and Then you can Set Array in NSMutableDictionary using setvalue(forKey:) default method.

var arrFname : NSArray!

arrFname = ["abc","xyz","mno"]

var arrLname : NSArray!

arrFname = ["tuv","xuv","swift"]

var dicSet : NSMutableDictionary!

dicSet.setObject(arrFname, forKey : "Fname")

dicSet.setObject(arrLname, forKey : "Lname")

print(dicSet)                                           
Rakesh
  • 756
  • 1
  • 9
  • 19
Berlin
  • 2,115
  • 2
  • 16
  • 28
-3
var tempDict = NSMutableDictionary()

tempDict.setValue("sam", forKey : "one")

print(tempDict["one"] ?? "1")
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
saurabh rathod
  • 1,090
  • 7
  • 7