【发布时间】:2018-10-11 10:27:16
【问题描述】:
我有一个结构,想要获取其中元素的计数并将值附加到数组中,但无法找到执行此操作的方法。
struct User_notification_preferences : Codable {
let comments : Bool?
let likes : Bool?
let dislikes : Bool?
let unfollow : Bool?
let follow : Bool?
let updates : Bool?
}
enum CodingKeys: String, CodingKey {
case follow = "follow"
case likes = "likes"
case unfollow = "unfollow"
case comments = "comments"
case updates = "updates"
case dislikes = "dislikes"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
follow = try values.decodeIfPresent(Bool.self, forKey: .follow)
unfollow = try values.decodeIfPresent(Bool.self, forKey: .unfollow)
likes = try values.decodeIfPresent(Bool.self, forKey: .likes)
dislike = try values.decodeIfPresent(Bool.self, forKey: .dislikes)
comments = try values.decodeIfPresent(Bool.self, forKey: .comments)
updates = try values.decodeIfPresent(Bool.self, forKey: .updates)
}
我使用它来显示和更新基于后端响应的 UISwitch 值。最好的方法是什么?下面是解码 JSON 响应后的输出,我希望将所需的输出作为字典 [[String:Bool]] 的数组。
【问题讨论】:
-
想要的结果是什么样的?
-
我已经编辑了我的问题以获取更多详细信息,谢谢!
-
switchStatus是字典而不是数组。如果属性User_notification_preferences为nil,是否应该在switchStatus的值中将其替换为false? -
希望它有一个字典数组,所以我可以根据它们在 tableview 中的索引来使用字典键和值。
-
你在问题中写它的方式,它是一本字典。您还没有回答您希望如何在这本字典中表示 nil 值?