如何检查 属性 值是否存在于 swift 中的对象数组中

how to check if a property value exists in array of objects in swift

我正在尝试检查对象数组中是否存在特定项目(属性 的值),但找不到任何解决方案。请让我知道,我在这里缺少什么。

        class Name {
            var id : Int
            var name : String
            init(id:Int, name:String){
                self.id = id
                self.name = name
            }
        }

        var objarray = [Name]()
        objarray.append(Name(id: 1, name: "Nuibb"))
        objarray.append(Name(id: 2, name: "Smith"))
        objarray.append(Name(id: 3, name: "Pollock"))
        objarray.append(Name(id: 4, name: "James"))
        objarray.append(Name(id: 5, name: "Farni"))
        objarray.append(Name(id: 6, name: "Kuni"))

        if contains(objarray["id"], 1) {
            println("1 exists in the array")
        }else{
            println("1 does not exists in the array")
        }

您可以像这样过滤数组:

let results = objarray.filter { [=10=].id == 1 }

这将 return 一个与闭包中指定的条件匹配的元素数组 - 在上述情况下,它将 return 一个包含具有 id 属性 等于 1.

由于您需要一个布尔结果,只需进行如下检查:

let exists = results.isEmpty == false
如果过滤后的数组至少有一个元素

,则

exists为真

这对我来说很好用:

if(contains(objarray){ x in x.id == 1})
{
     println("1 exists in the array")
}

Swift2.x:

if objarray.contains({ name in name.id == 1 }) {
    print("1 exists in the array")
} else {
    print("1 does not exists in the array")
}

Swift 3:

if objarray.contains(where: { name in name.id == 1 }) {
    print("1 exists in the array")
} else {
    print("1 does not exists in the array")
}

使用 , (where) 符号对@Antonio 的解决方案进行小迭代:

if let results = objarray.filter({ [=10=].id == 1 }), results.count > 0 {
   print("1 exists in the array")
} else {
   print("1 does not exists in the array")
}

我用这个解决方案来解决类似的问题。使用包含 returns 一个布尔值。

var myVar = "James"

if myArray.contains(myVar) {
            print("present")
        }
        else {
            print("no present")
        }

签名:

let booleanValue = 'propertie' in yourArray;

示例:

let yourArray= ['1', '2', '3'];

let contains = '2' in yourArray; => true
let contains = '4' in yourArray; => false

//Swift 4.2

    if objarray.contains(where: { [=10=].id == 1 }) {
        // print("1 exists in the array")
    } else {
        // print("1 does not exists in the array")
    }

这里主要有2个可行的选项。

  1. objarray 上使用 contains(where: 方法。我只是在这里对@TheEye 的回答做一个小修改:
if objarray.contains(where: { [=10=].id == 1 }) {
    print("1 exists in the array")
} else {
    print("1 does not exists in the array")
}
  1. 覆盖 Equatable 协议一致性要求并仅使用 contains,如下所示:
// first conform `Name` to `Equatable`
extension Name: Equatable {
    static func == (lhs: Name, rhs: Name) -> Bool {
        lhs.id == rhs.id
    }
}
// then
let firstName = Name(id: 1, name: "Different Name")
if objarray.contains(firstName) {
    print("1 exists in the array") // prints this out although the names are different, because of the override
} else {
    print("1 does not exists in the array")
}
struct Name {
    var id = Int()
    var name = String()
}

var objarray = [Name]()
objarray.append(Name(id: 1, name: "Nuibb"))
objarray.append(Name(id: 2, name: "Smith"))
objarray.append(Name(id: 3, name: "Pollock"))
objarray.append(Name(id: 4, name: "James"))
objarray.append(Name(id: 5, name: "Farni"))
objarray.append(Name(id: 6, name: "Kuni"))

var oneExists = !objarray.filter{ [=10=].id == 1 }.isEmpty