本質上,我一直在創建以下簡單的 String/Int 陣列以將資訊記錄到陣列集中(當然,在追加之前檢查重復項)如何在字串旁邊記錄 ID 以創建 JSON 陣列?
目前正在做以下作業:
var myArray = Set<String>()
myArray.insert("Example1")
["Example1","Example2","Example3"]
我想要實作的示例。
[{"myexample":"Example2","id":1},{"myexample":"Example3","id":2}]
uj5u.com熱心網友回復:
您可以使用struct符合Codable來表示您的資料:
struct Model : Codable, Hashable {
var myexample: String
var id: Int
}
var mySet = Set([Model(myexample: "Example2", id: 1),
Model(myexample: "Example3", id: 2)])
mySet.insert(Model(myexample: "Example4", id: 3))
do {
let json = try JSONEncoder().encode(mySet)
print(String(data: json, encoding: .utf8)!)
} catch {
print(error)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/410744.html
標籤:
上一篇:UITextfield陰影
下一篇:如何將文本檔案轉換為dict?
