For example, I have a json response of array type where the second key in each array has same value. The count of the array is dynamic but I need to check everytime, if that particular key value is same in all the arrays, i need to hide a label.
"loadable": [
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
{
"position": {
"positionType": "XXX",
"thirdKey": 1,
"fourthKey": 1,
},
}
}
]
Здесь я хочу проверить, все ли значения для ключевого positionType == "XXX", тогда мне нужно скрыть метку. Пожалуйста, ответьте быстро.
Если вы хотите проверить, что каждое значение равно в массиве, вы можете сделать это так.
let searchValue = "xxx"
if loadables.index(where: {$0.position?.positionType != searchValue}) != nil {
//positionType for all objects are not equal to searchValue
footer.labelTitle.isHidden = false
}
else {
//positionType for all objects are equal to searchValue
footer.labelTitle.isHidden = true
}
Это меньше проблема json, но вопрос "Все элементы в моем словаре имеют одинаковое значение", где элементы будут ключевым элементом, который вы ищете.
func checkValues(array: Array<Element>) -> Bool {
guard let myValue = array.firstObject()?.position?.positionType else {
return false
}
for element in array {
if element.position.positionType != myValue {
return false
}
}
return true
}