Compare 2 arrays and list the differences - Swift

前端 未结 3 1316
再見小時候
再見小時候 2020-12-11 01:41

I was wondering how one would go about comparing 2 boolean arrays and listing the non matching booleans.

I have written up a simple example of 2 arrays.



        
3条回答
  •  一个人的身影
    2020-12-11 02:19

    Xcode 11 has it supported

    https://developer.apple.com/documentation/swift/array/3200716-difference

    let oldNames = ["a", "b", "c", "d"]
    
    let newNames = ["a", "b", "d", "e"]
    
    let difference = newNames.difference(from: oldNames)
    
    for change in difference {
      switch change {
      case let .remove(offset, oldElement, _):
        print("remove:", offset, oldElement)
      case let .insert(offset, newElement, _):
        print("insert:", offset, newElement)
      }
    }
    

    Output

    remove: 2 c
    insert: 3 e
    

提交回复
热议问题