【发布时间】:2017-12-21 10:06:27
【问题描述】:
我对如何使用 Alamofire 发布这个 json 数组有点困惑。我看过How can I use Swift’s Codable to encode into a dictionary? 和其他一些人,但我似乎无法让它发挥作用。
我正在从 UITableView 中附加几行,在编码之前它看起来像这样。
[proj.DetailViewModel(Quantity: 1, RedeemedLocationID: 6, Points: 10),
proj.DetailViewModel(Quantity: 2, RedeemedLocationID: 6, Points: 12)]
struct DetailViewModel: Codable {
var Quantity: Int!
var RedeemedLocationID: Int!
var Points: Int!
}
var selectedAwards = [DetailViewModel]()
let jsonData = try JSONEncoder().encode(selectedAwards)
// error nil
let dict = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any]
// error nil
let struct1 = selectedAwards
let dict = try struct1.asDictionary()
如果我使用 SwiftyJson 只是为了检查它看起来像这样
let json = JSON(jsonData)
print(json)
[
{
"Points" : 10,
"Quantity" : 1,
"RedeemedLocationID" : 6
},
{
"Points" : 12,
"Quantity" : 2,
"RedeemedLocationID" : 6
}
]
【问题讨论】:
标签: swift alamofire swift4 codable