在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 内置函数:在Swift中不需要导入任何模块(如UIKit等)或者引用任何类就可以使用的函数。Swift预先定义的函数 1、断言assert和先决条件precondition 使用断言进行调试您可以通过assert(_:_:file:line:)从Swift标准库中调用函数来编写断言。您将此函数传递给一个表达式,该表达式求值为true或者false,如果条件的结果为,则显示一条消息false。例如: 1 let age = -3 2 assert(age >= 0, "A person's age can't be less than zero.") 3 // This assertion fails because -3 is not >= 0. 在此示例中,如果求值,则代码执行继续,即,值是否为非负值。如果值为负,如上面的代码所示,则求值为,并且断言失败,终止应用程序。 assert(age >= 0)
如果代码已经检查了条件,则使用该 1 if age > 10 { 2 print("You can ride the roller-coaster or the ferris wheel.") 3 } else if age >= 0 { 4 print("You can ride the ferris wheel.") 5 } else { 6 assertionFailure("A person's age can't be less than zero.") 7 } 执行先决条件只要条件可能为false,就使用先决条件,但对于代码继续执行必须确实为真。例如,使用先决条件检查下标是否超出范围,或检查函数是否已传递有效值。 你通过调用 1 // In the implementation of a subscript... 2 precondition(index > 0, "Index must be greater than zero.") 您也可以调用该 注意 如果以unchecked mode( 您可以 2、count函数:获取序列的元素个数 1 let str:String = "strengthen" 2 print(str.count) 3 //Pring: 10 4 print(str.characters.count) 5 //Pring: 10 6 let arr:[Int] = [1,2,3,4,5] 7 print(arr.count) 8 //Pring: 5 3、enumerated() 将原有序列转换成以元组作为元素的序列输出 1 let arr = ["s","t","r","e","n","g","t","h","e","n"] 2 //打印新序列 3 for (i,j) in arr.enumerated() 4 { 5 print("\(i):\(j)") 6 } 7 //0:s 8 //1:t 9 //2:r 10 //3:e 11 //4:n 12 //5:g 13 //6:t 14 //7:h 15 //8:e 16 //9:n 4、max() 返回多个元素的最大值 1 /*min()*/ 2 //方式1 3 let num1 = min(9,1) 4 print(num1) 5 //Print 1 6 let num2 = min(9,7,5,3,1) 7 print(num2) 8 //Print 1 9 10 let arr = [3,1,5,9] 11 //方式2 数组 12 let num4 = arr.min { (a, b) -> Bool in 13 a < b 14 } 15 print(num4) 16 //Print Optional(1) 17 18 /*max()*/ 19 //方式1 20 let num1 = max(9,1) 21 print(num1) 22 //Print 9 23 let num2 = max(9,7,5,3,1) 24 print(num2) 25 //Print 9 26 27 let arr = [3,1,5,9] 28 //方式2 数组 29 let num4 = arr.max { (a, b) -> Bool in 30 a < b 31 } 32 print(num4) 33 //Print Optional(9) 5、map函数:对数组/序列中的元素进行特定操作,返回一个新的数组/序列。 map函数能够被数组调用,它接受一个闭包作为参数,作用于数组中的每个元素,闭包返回一个变换后的元素,接着将所有这些变换后的元素组成一个新的数组。 1 let arr = [1,2,3,4,5,6,7,8,9] 2 3 //数组各元素放大2倍 4 let doubleArr = arr.map {$0 * 2} 5 print(doubleArr) 6 //Print [2, 4, 6, 8, 10, 12, 14, 16, 18] 7 8 //Int数组各元素放大3倍,再转换为String 9 let moneyArr = arr.map { "$\($0 * 3)"} 10 print(moneyArr) 11 //Print ["$3", "$6", "$9", "$12", "$15", "$18", "$21", "$24", "$27"] 12 13 //数组转元组 14 let groupArr = arr[0...5].map{($0, "\($0)")} 15 print(groupArr) 16 //Print [(1, "1"), (2, "2"), (3, "3"), (4, "4"), (5, "5"), (6, "6")] 6、flapMap降低数组维度,过滤数组中的nil值。 1 let arr1 = [[1,2],[3,4],[5,6],[7,8,9]] 2 3 //flapMap函数降低维度 4 let flapMapAry1 = arr1.flatMap{$0} 5 print(flapMapAry1) 6 //Print [1, 2, 3, 4, 5, 6, 7, 8, 9] 7 8 //flapMap函数降低维度 9 let arr2 = [[[1,1,1],2],[3,4],[5,6],[7,8,[9,9,9]]] 10 //flapMap函数降低维度 11 let flapMapAry2 = arr2.flatMap{$0} 12 print(flapMapAry2) 13 //Print [[1, 1, 1], 2, 3, 4, 5, 6, 7, 8, [9, 9, 9]] 14 15 //flapMap函数会过滤数组中的nil值 16 let arr3:[Int?] = [1, 2, 3, nil, nil] 17 let flapMapAry3 = arr3.flatMap{$0} 18 print(flapMapAry3) 19 //Print [1, 2, 3] 7、filter筛选函数:序列/数组的元素进行筛选,筛选的结果放入一个新的数组 1 let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] 2 3 //方式1 4 //筛选偶数值 5 let even = numbers.filter{(num) -> Bool in 6 num % 2 == 0 7 } 8 print("\(even) ", terminator: "") 9 //Print [2, 4, 6, 8] 10 11 //筛选奇数值 12 let odd = numbers.filter{(num) -> Bool in 13 num % 2 == 1 14 } 15 print("\(odd) ", terminator: "") 16 //Pring [1, 3, 5, 7, 9] 17 18 //方式2 19 //筛选偶数值 20 let evens = numbers.filter{$0 % 2 == 0} 21 print("\(evens) ", terminator: "") 22 //Print [2, 4, 6, 8] 23 24 //筛选奇数值 25 let odds = numbers.filter{$0 % 2 == 1} 26 print("\(odds) ", terminator: "") 27 //Pring [1, 3, 5, 7, 9] 8、reduce():给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值。 1 let arr = [1,2,3,4,5] 2 //方式1 3 let res = arr.reduce(0){(res, item) -> Int in 4 res + item 5 } 6 print("\(res) ", terminator: "") 7 //Print 15 8 9 //方式2 10 //数组各元素求和reduce(0) 11 let sum = arr.reduce(0) {$0 + $1} 12 print("\(sum) ", terminator: "") 13 //Print 15 14 //数组各元素求积reduce(1) 15 let product = arr.reduce(1) {$0 * $1} 16 print("\(product) ", terminator: "") 17 //Print 120 18 19 //方式3 20 let arr2 = ["1","2","3","4","5"] 21 //方式2:数组转字符串 22 let str = arr2.reduce("", {$0 + $1}) 23 print("\(str) ", terminator: "") 24 //Print 12345 9、绝对值函数 1 //小数位5个9 2 print(abs(9.999999)) 3 print(fabs(9.999999)) 4 print(fabsf(9.99999)) 5 //Print 9.999999 6 7 //小数位6个9 8 print(fabsf(9.999999)) 9 //Print 1.0 10、dropFirst(): 返回一个去掉第一个元素的新序列/数组 1 //初始化一个数组 2 var arr = ["s","t","r","e","n","g","t","h","e","n"] 3 //删除第一位元素,变成ArraySlice<String>类型 4 var arr2:ArraySlice<String> = arr.dropFirst() 5 //ArraySlice<String>类型转换为[String]类型 6 var arr3 = [String](arr.dropFirst()) 7 print(arr2) 8 //Print ["t", "r", "e", "n", "g", "t", "h", "e", "n"] 9 print(arr3) 10 //Print ["t", "r", "e", "n", "g", "t", "h", "e", "n"] 11、dropLast(): 返回一个的新序列/数组,该序列去掉作为参数传递给函数的最后一个元素。 1 //初始化一个数组 2 var arr = ["s","t","r","e","n","g","t","h","e","n"] 3 //删除第一位元素,变成ArraySlice<String>类型 4 var arr2:ArraySlice<String> = arr.dropLast() 5 //ArraySlice<String>类型转换为[String]类型 6 var arr3 = [String](arr.dropLast()) 7 print(arr2) 8 //Print ["s", "t", "r", "e", "n", "g", "t", "h", "e"] 9 print(arr3) 10 //Print ["s", "t", "r", "e", "n", "g", "t", "h", "e"] 12、dump(object): 一个对象的内容转储到标准输出。 1 //初始化一个数组 2 var arr = ["s","t","r","e","n","g","t","h","e","n"] 3 dump(arr) 4 //Print 5 ▿ 10 elements 6 - "s" 7 - "t" 8 - "r" 9 - "e" 10 - "n" 11 - "g" 12 - "t" 13 - "h" 14 - "e" 15 - "n" 13、elementsEqual(other: Sequence): 判断两个序列/数组中的元素是否完全相等,元素的顺序也必须相同。 1 //初始化一个数组 2 var arr1 = ["s","t","r","e","n","g","t","h","e","n"] 3 var arr2 = ["s","t","r","e","n","g","t","h","e","n"] 4 //判断两个序列/数组中的元素是否完全相等,元素的顺序也必须相同 5 let flag = arr1.elementsEqual(arr2) 6 print(flag) 7 //Print true 14、contains() 序列/数组是否包含某个元素:如果某个序列sequence(比如说一个数组)包含指定的元素element,则返回true,否则返回false 1 //初始化一个数组 2 var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] 3 4 //方式1:闭包拓展性强 5 let flag = arr.contains { (num) -> Bool in 6 //判断是否包含偶数 7 num % 2 == 0 8 } 9 print(flag) 10 //Print true 11 12 //方式2:直接 13 print(arr.contains(7)) 14 //Print true 15、joined(separator: String)序列\数组拼接字符串,对数组的元素顺序拼接,并插入指定字符串 1 var arr = ["s","t","r","e","n","g","t","h","e","n"] 2 let str = arr.joined(separator: "-") 3 print(str) 4 //Print s-t-r-e-n-g-t-h-e-n 16、sorted排序,sorted()默认是升序,.sorted(by: (Int, Int) -> Bool)自定义排序 1 //方式1 2 //sorted()默认是升序 3 let arr1 = arr.sorted() 4 print(arr1) 5 //Print [1, 2, 3, 4, 5] 6 7 //方式2 8 //降序 9 let arr2 = arr.sorted { (a, b) -> Bool in 10 a > b 11 } 12 //简写形式 13 let arr3 = arr.sorted{$0 > $1} 14 //最简形式 15 let arr4 = arr.sorted(by: >) 16 print(arr2) 17 print(arr3) 18 print(arr4) 19 //Print [5, 4, 3, 2, 1] 20 21 //升序 22 let arr5 = arr.sorted { (a, b) -> Bool in 23 a < b 24 } 25 //简写形式 26 let arr6 = arr.sorted{$0 < $1} 27 //最简形式 28 let arr7 = arr.sorted(by: <) 29 print(arr5) 30 print(arr6) 31 print(arr7) 32 //Print [1, 2, 3, 4, 5] 17、indices: 在指定的序列中返回元素的索引(零索引),可用于字符串遍历 1 let arrayStr:[String] = ["s","t","r", |
请发表评论