如何在 swift 中计算 JSON 响应中的布尔值



如何计算 JSON 响应中的布尔值?.我首先做了总消息计数,然后使用 if 语句只获取假值。

  • 我只希望计算假值。messageReaded = false计数

法典:

let response = try decoder.decode(ResponseData.self, from: result! as! Data)
if response.isSuccess == true {
self.AppMsg = response.messageList ?? []
let msgCount = String(describing: self.AppMsg.count)
if msgCount.isEmpty == false {
for i in response.messageList ?? [] {
if i.messageReaded == false {
}
}    
}
}

响应:

{
"messageList": [
{
"messageId": 22,
"messageReaded": false,
"messageDate": "27.05.2020 07:01",
"title": "New Message",
"messageTypeCode": 1
},
{
"messageId": 21,
"messageReaded": true,
"messageDate": "19.05.2020 07:00",
"title": "Old Message",
"messageTypeCode": 1
}
],
"isSuccess": true,
"message": "İşlem Başarılı.",
"statusCode": 1
}

过滤带有条件messageReaded == false的消息并count结果。

let response = try decoder.decode(ResponseData.self, from: result! as! Data)
if response.isSuccess {
self.AppMsg = response.messageList ?? [] 
let unreadCount = self.AppMsg.filter{$0.messageReaded == false}.count
}

在迭代之前,没有必要检查数组是否为空。如果数组为空,则将跳过循环。

最新更新