Compare arrays in swift

前端 未结 6 1228
死守一世寂寞
死守一世寂寞 2020-11-30 02:59

Trying to understand how swift compares arrays.

var myArray1 : [String] = [\"1\",\"2\",\"3\",\"4\",\"5\"]
var myArray2 : [String] = [\"1\",\"2\",\"3\",\"4\",         


        
6条回答
  •  执念已碎
    2020-11-30 03:20

    It depends on how do you want to compare. For example: ["1", "2"] == ["1", "2"] // true but ["1", "2"] == ["2", "1"] // false

    If you need that second case to also be true and are ok with ignoring repetitive values, you can do: Set(["1", "2"]) == Set(["2", "1"]) // true (use NSSet for Swift 2)

提交回复
热议问题