【问题标题】:Append the struct elemnts in a array将结构元素附加到数组中
【发布时间】: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 值?

标签: ios swift struct codable


【解决方案1】:

您可以在键值对中对其进行编码,然后获取您想要的任何内容:

let userNotiPre  = User_notification_preferences()
let jsonDatas = try JSONEncoder().encode(userNotiPre)

if let dict = try JSONSerialization.jsonObject(with: jsonDatas, options: []) as? [String: Hashable]{
   //Get all values from dict and then you can also get count of that array 
   let array = dict.values
   array.count
}

【讨论】:

    【解决方案2】:

    你可以用镜子:

    let pref = User_notification_preferences(comments: true, likes: false, dislikes: nil, unfollow: true, follow: nil, updates: false)
    
    let prefMirror = Mirror(reflecting: pref)
    
    var switchStatus: [String:Bool] = [String:Bool]()
    
    prefMirror.children.forEach { child in
        guard let label = child.label else {
            fatalError("Couldn't get the label")
        }
        switchStatus[label] = child.value as? Bool ?? false
    }
    
    print(switchStatus) //["follow": false, "updates": false, "unfollow": true, "dislikes": false, "likes": false, "comments": true]
    

    即使您修改User_notification_preferences 的属性,这也将起作用。

    或者,您可以使用这样的简单函数:

    struct User_notification_preferences : Codable {
        //...
        func dictionaryRepresentation() -> [String:Bool] {
            return ["comments": comments ?? false,
                    "likes": likes ?? false,
                    "dislikes": dislikes ?? false,
                    "unfollow": likes ?? false,
                    "follow": likes ?? false,
                    "updates": likes ?? false
            ]
        }
    }
    

    然后像这样使用它:

    print(pref.dictionaryRepresentation()) //["follow": false, "updates": false, "comments": true, "likes": false, "dislikes": false, "unfollow": false]
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2021-07-04
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2018-08-18
      • 2014-02-24
      相关资源
      最近更新 更多