Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检查特定的json键是否具有与所有键相同的值_Ios_Arrays_Json_Swift_Swift3 - Fatal编程技术网

Ios 如何检查特定的json键是否具有与所有键相同的值

Ios 如何检查特定的json键是否具有与所有键相同的值,ios,arrays,json,swift,swift3,Ios,Arrays,Json,Swift,Swift3,这里我想检查key positionType的所有值是否为“XXX”,然后我需要隐藏一个标签。请用swift提供答案。如果要检查数组中的每个值是否相等,则可以这样设置 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,

这里我想检查key positionType的所有值是否为“XXX”,然后我需要隐藏一个标签。请用swift提供答案。

如果要检查数组中的每个值是否相等,则可以这样设置

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,
        },
}
}
]

这不是一个json问题,而是一个“我的字典中的所有元素都有相同的值”的问题,其中元素将是您要查找的键

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
}
func校验值(数组:数组)->Bool{
guard let myValue=array.firstObject()?.position?.positionType else{
返回错误
}
对于数组中的元素{
如果element.position.positionType!=myValue{
返回错误
}
}
返回真值
}

“请用swift提供答案。”到目前为止,您尝试了什么?对于可加载项中的可加载项{if loadable.position?.positionType==“XXX”{footer.labelTitle.ishiden=true}现在我得到了4个数组,其中所有的值都是XXX。但是我尝试的代码被设置为true,即使有一个XXX,并且XXXloadables以外的任何其他值都是可加载类的变量,该类是NSManagedObject。var loadables:[Loadable]=[]@@vinny显示可加载的声明,然后如果任何键与“XXX”匹配,则此操作正常。但是如果所有键值都等于“XXX”,我想隐藏标签。如果至少有一个值不是XXX,我想显示label@vinny那么这个解决方案在这种情况下非常有效,还可以检查我是否编辑了答案try once编辑了答案Thank a ton.。这就解决了问题:)@niravD@vinny欢迎朋友:)
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
}