Reputation: 149
How can I count Boolean value from JSON response?. I made firstly total message count and then use if statement to get only false values.
messageReaded = false
Code:
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 {
}
}
}
}
Response:
{
"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
}
Upvotes: 1
Views: 321
Reputation: 285069
Filter the messages with condition messageReaded == false
and count
the result.
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
}
It's not necessary to check if an array is empty before being iterated. If the array is empty the loop will be skipped.
Upvotes: 1