ios 替换数组中元素,iOS swift从另一个数组中删除数组的元素
I have two arraysvar array1 = new Array ["a", "b", "c", "d", "e"]var array2 = new Array ["a", "c", "d"]I want to remove elements of array2 from array1Result ["b", "e"]解决方案The easiest way is to convert
I have two arrays
var array1 = new Array ["a", "b", "c", "d", "e"]
var array2 = new Array ["a", "c", "d"]
I want to remove elements of array2 from array1
Result ["b", "e"]
解决方案
The easiest way is to convert both arrays to sets, subtract the second from the first, convert to the result to an array and assign it back to array1:
array1 = Array(Set(array1).subtracting(array2))
Note that your code is not valid Swift - you can use type inference to declare and initialize both arrays as follows:
var array1 = ["a", "b", "c", "d", "e"]
var array2 = ["a", "c", "d"]
更多推荐
所有评论(0)