简体   繁体   English

将元素添加到空字典,然后添加到数组

[英]Add elements to an empty dictionary and then add to array

I have two string elements.我有两个字符串元素。 I want to add them to a dictionary and then finally, add the dictionary to an array.我想将它们添加到字典中,然后最后将字典添加到数组中。 I tried doing it like so..But it's not working...我试着这样做......但它不起作用......

I made a dictionary like so..我做了一本这样的字典..

var receiverDict = Dictionary<String, Any>()

The 2 strings that I have are these..我拥有的 2 个字符串是这些..

self.abc_Receiver1 = "test1" & self.abc_Receiver1 = "test1" &

self.abc_Receiver2 = "test2"

I tried to add it to the dictionary like so...我试着像这样将它添加到字典中......

(self.receiverDict["name"]! as AnyObject).add(self.Smat_Receiver1) //HERE IT CRASHES
(self.receiverDict["id"]! as AnyObject).add(self.receiverId1) 

But it crashes in the first line above.但它在上面的第一行崩溃了。

Further, I had also made an array like so to add the above dictionary to this arraylike this...此外,我还制作了一个这样的数组,将上面的字典添加到这个数组中......

var arrayOfReceiverNames = [[String: Any]]()

arrayOfReceiverNames.append(self.receiverDict)

But the crash is occuring while adding elements to the dictionary itself.但是在向字典本身添加元素时发生崩溃。

Please, this is Swift.拜托,这是斯威夫特。 Your dictionary is clearly [String:String]你的字典显然是[String:String]

var receiverDict = Dictionary<String, String>()

Then use native Swift terminology without bridging to the Objective-C runtime and add is wrong anyway.然后使用原生 Swift 术语而不桥接到 Objective-C 运行时,无论如何add都是错误的。

receiverDict["name"] = self.Smat_Receiver1
receiverDict["id"] = self.receiverId1

Or simpler in one line或者更简单的一行

let receiverDict = ["name" : self.Smat_Receiver1, "id" : self.receiverId1] 

Declare the array also more specific声明数组也更具体

var arrayOfReceiverNames = [[String: String]]()
arrayOfReceiverNames.append(receiverDict)

You can achieve it by doing:您可以通过执行以下操作来实现它:

var dict = [String:Any]()
var arrayOfDict = Array<[String:Any]>()

dict["name"] = someValue

arrayOfDict.append(dict)

Hope it helps.希望能帮助到你。

If you already know the strings and the values, why not directly add them like below:如果您已经知道字符串和值,为什么不直接添加它们,如下所示:

var arrayOfDict : [Dictionary<String,String>] = [["name1":"value1"], ["name2":"value2"]]

If you have them stored in a variable, just replace the string with your variable.如果您将它们存储在变量中,只需将字符串替换为您的变量即可。

So in your case,所以在你的情况下,

 self.abc_Receiver1 = "test1"
 self.abc_Receiver2 = "test2"
 var arrayOfReceiverNames : [Dictionary<String,String>] = [["id":self.receiverId1], ["name":self.Smat_Receiver1]]

If you've values set for both Smat_Receiver1 and abc_Receiver2 before creating the Dictionary , then you can simply use a one liner to create your Dictionary , ie如果您在创建Dictionary之前为Smat_Receiver1abc_Receiver2设置了值,那么您可以简单地使用 one liner 来创建您的Dictionary ,即

let receiverDict = ["name": self.Smat_Receiver1, "id": self.abc_Receiver2]

The type of receiverDict will be inferred automatically as [String:String] . receiverDict的类型将被自动推断为[String:String]

Now, create the array with receiverDict as,现在,使用receiverDict创建array

var arrayOfReceiverNames = [[String:String]]()
arrayOfReceiverNames.append(receiverDict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM