gpt4 book ai didi

ios - 在swift 4中创建一个包含数组和字典的字典

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:11 25 4
gpt4 key购买 nike

我只想创建一个 API JSON 结构。以下是帖子正文键和对象。在 Swift 4 中是否有类似于 Objective C 的带有键和值的对象的方法?

{
"name": "switch 1",
"type": "Switch",
"gatewayId":515,
"serialKey": "98:07:2D:48:D3:56",
"noOfGangs": 4,
"equipments": [
{
"name": "light",
"type": "Light",
"port": "1"
},
{
"name": "television",
"type": "Television",
"port": "3"
}
]
}

最佳答案

您可以通过注释类型并用方括号替换大括号来按字面意义创建字典

let dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4, "equipments": [[ "name": "light", "type": "Light", "port": "1" ], ["name": "television", "type": "Television", "port": "3" ]]]

或者构建它:

var dict : [String:Any] = ["name": "switch 1", "type": "Switch", "gatewayId":515, "serialKey": "98:07:2D:48:D3:56", "noOfGangs": 4]
var equipments = [[String:String]]()
equipments.append(["name": "light", "type": "Light", "port": "1" ])
equipments.append(["name": "television", "type": "Television", "port": "3" ])
dict["equipments"] = equipments

关于ios - 在swift 4中创建一个包含数组和字典的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45810587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com