swift:向NSDictionary中添加多个对象

时间:2021-06-26 13:49:48

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

我在尝试向NSDictionary添加多个对象,比如

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.

有可能这样做吗?如果没有,请建议更好的方法。实际上我需要将NSDictionary转换为JSON字符串并将其发送到服务器,所以我需要NSDictionary中的多个对象。

3 个解决方案

#1


43  

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

得到这样的JSON。

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

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

您需要将上述数组添加到字典中,如下所示:

var myDict:NSDictionary = ["Data" : myArrayOfDict]

#2


0  

SWIFT 3.0

斯威夫特3.0

  • List item
  • 列表项

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

首先创建NSArray,然后使用setvalue(forKey:)默认方法在NSMutableDictionary中设置数组。

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)                                           

#3


-2  

var tempDict = NSMutableDictionary()

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

print(tempDict["one"] ?? "1")

#1


43  

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

得到这样的JSON。

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

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

您需要将上述数组添加到字典中,如下所示:

var myDict:NSDictionary = ["Data" : myArrayOfDict]

#2


0  

SWIFT 3.0

斯威夫特3.0

  • List item
  • 列表项

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

首先创建NSArray,然后使用setvalue(forKey:)默认方法在NSMutableDictionary中设置数组。

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)                                           

#3


-2  

var tempDict = NSMutableDictionary()

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

print(tempDict["one"] ?? "1")