【问题标题】:how to check if a property value exists in array of objects in swift如何快速检查对象数组中是否存在属性值
【发布时间】:2015-03-28 11:15:24
【问题描述】:

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

        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")
        }

【问题讨论】:

    标签: swift properties contains


    【解决方案1】:

    你可以像这样过滤数组:

    let results = objarray.filter { $0.id == 1 }
    

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

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

    let exists = results.isEmpty == false
    

    如果过滤后的数组至少有一个元素,exists 将为真

    【讨论】:

      【解决方案2】:

      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")
      }
      

      【讨论】:

      • 比较成功后如何返回对象?
      • @Hemang 如果您需要的不仅仅是测试条件,请使用filter 而不是contains
      • 如何在 where: 子句下再检查一个条件?
      【解决方案3】:

      Swift 2.x 中:

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

      【讨论】:

        【解决方案4】:

        //斯威夫特4.2

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

            这里主要有两个可行的选择。

            1. objarray 使用contains(where: 方法。我只是在这里对@TheEye 的答案进行了小修改:
            if objarray.contains(where: { $0.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")
            }
            

            【讨论】:

              【解决方案7】:

              这对我来说很好用:

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

              【讨论】:

                【解决方案8】:

                签名:

                let booleanValue = 'propertie' in yourArray;
                

                示例:

                let yourArray= ['1', '2', '3'];
                
                let contains = '2' in yourArray; => true
                let contains = '4' in yourArray; => false
                

                【讨论】:

                • 我相信问题是“objects 数组”而不是“string 数组”,正如您提供的示例。所以这实际上并没有解决所提出的问题。
                【解决方案9】:
                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{ $0.id == 1 }.isEmpty
                

                【讨论】:

                  【解决方案10】:

                  我用这个解决方案来解决类似的问题。使用 contains 返回一个布尔值。

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

                  【讨论】:

                    猜你喜欢
                    • 2020-11-30
                    • 2012-07-27
                    • 1970-01-01
                    • 2016-09-24
                    • 1970-01-01
                    • 2016-01-17
                    • 1970-01-01
                    • 2019-02-16
                    • 2017-03-09
                    相关资源
                    最近更新 更多