swift 3 以编程方式填充字典

swift 3 fill dictionary programmatically

我为 .post 请求填写我的参数,如下所示:

params = ["title":msgTitle.text ?? "",
"hashTags": [["name":"tag1"],["name":"HashTag"]]] as [String : Any]

如您所见,哈希标签是静态的。我需要帮助来理解字典。有没有办法通过将它们附加到字典来设置哈希标签?我希望能够添加多个 "name":"hashtagName" 对。

感谢任何帮助!

是的,你可以,例如创建字典

var params: [String : Any] = ["title":msgTitle.text ?? ""]

然后设置键值 hashtags

params["hashTags"] = [["name":"tag1"],["name":"HashTag"]]

或通过数组

var hashtags = [["name":"tag1"]]
hashtags.append(["name":"HashTag"])
params["hashTags"] = hashtags

或者反过来:

var params: [String : Any] = ["hashTags" : [["name":"tag1"],["name":"HashTag"]]]
params["title"] = msgTitle.text ?? ""