Compare value from two struct in swift

后端 未结 4 1524
梦毁少年i
梦毁少年i 2021-01-25 18:09

I have two struct and two arrays corresponding to it and I am trying to compare the two array values and print it in one filtered array i did try and use filter but its giving m

4条回答
  •  -上瘾入骨i
    2021-01-25 18:37

    Swift standard library has an Equatable protocol that we can adopt by adding the static == operator function to our type in an extension. You should create only one structure like below

    struct Employee {
      let id: String
      let name: String
      var lastName: String
    }
    
    extension Employee: Equatable {
      static func == (lhs: Employee, rhs: Employee) -> Bool {
        return lhs.name == rhs.name &&
        lhs.id == rhs.id &&
        lhs.lastName == rhs.lastName
      }
    }
    

提交回复
热议问题