标签:nil nta contains 从后往前 索引 ret dex fir bool
1. filter (过滤器):返回符合条件的一个数组 let arr = [1,5,6,7,10,0] //写法1 let arr1 = arr.filter { (item) -> Bool in return item % 2 == 0 } //写法2 let resultArray = arr.filter { return $0%2 == 0 } //写法3 let arr2 = arr.filter({return $0%2 == 0})
2.index(符合条件的索引):返回int值 let arr = [1,5,6,7,10,0] //写法1:swift 4.0 let index = arr.index(where: {$0 == 1}) //swift4.2 firstIndex:从前往后遍历, let index2 = arr.firstIndex(where:{$0 == 1}) let index3 = arr.firstIndex { (item) -> Bool in return item == 1 } //lastIndex:c从后往前遍历 let index4 = arr.lastIndex(where:{$0 == 1}) let index5 = arr.lastIndex(of: 5) if index5 != nil{ print("打印一下") }
3. contains(包含):返回bool值 let arr = [1,5,6,7,10,0] //写法1 let isContains = arr.contains(where: {$0 == 3}) //写法2 let isContains1 = arr.contains { (item) -> Bool in return item == 100 } //写法3 直接调用函数 arr.contains(10) print(arr.contains(3))
标签:nil nta contains 从后往前 索引 ret dex fir bool
原文地址:https://www.cnblogs.com/qingzZ/p/9835821.html