常用的 74个内置函数

  1 // MARK: 1.断言 assert,参数如果为ture则继续,否则抛出异常
  2 let number = 3
  3 
  4 //第一个参数为判断条件,第二各参数为条件不满足时的打印信息
  5 assert(number >= 3,"number 不大于 3")
  6 
  7 //如果断言被处罚(number <= 3),将会强制结束程序,并打印相关信息
  8 // assertion failed: number 不大于 3
  9 // 断言可以引发程序终止,主要用于调试阶段。比如下面的情况:
 10 /*
 11 * 自定义整形数组索引越界问题
 12 * 向函数传值,无效值引发函数不能完成相应任务
 13 * Optional类型数据为nil值,引发的程序crash
 14 */
 15 
 16 // MARK: 2.获取序列的元素个数 characters.count (countElements)
 17 let str = "foo"
 18 
 19 //打印元素个数
 20 print("count == \(str.characters.count)")
 21 //打印结果
 22 // count == 3
 23 
 24 // MARK: 3.返回最大最小值min(),max()
 25 max(10, 20)
 26 min(1, 3, 5, 9, 8)
 27 
 28 // MARK: 4.排序 sorted (sort)
 29 let ary  = ["B", "A", "C"]
 30 
 31 //默认排序(升序)
 32 let ary1 = ary.sorted()
 33 print(ary1)
 34 //打印结果
 35 // ["A", "B", "C"]
 36 
 37 //自定义排序(降序)
 38 let ary2 = ary.sorted {
 39 //    $1 < $0
 40     $0.1 < $0.0
 41 }
 42 print(ary2)
 43 //打印结果
 44 // ["C", "B", "A"]
 45 // 可以看出,可以用0、1、2来表示调用闭包中参数,0指代第一个参数,1指代第二个参数,2指代第三个参数,以此类推n+1指代第n个参数,后的数字代表参数的位置,一一对应。
 46 
 47 // MARK: 5.map函数
 48 let arr = [2,1,3]
 49 
 50 //数组元素进行2倍放大
 51 let doubleArr = arr.map {$0 * 2}
 52 print(doubleArr)
 53 //打印结果
 54 //[4, 2, 6]
 55 
 56 //数组Int转String
 57 let moneyArr = arr.map { "¥\($0 * 2)"}
 58 print(moneyArr)
 59 //打印结果
 60 //["¥4", "¥2", "¥6"]
 61 
 62 //数组转成元组
 63 let groupArr = arr.map {($0, "\($0)")}
 64 print(groupArr)
 65 //打印结果
 66 //[(2, "2"), (1, "1"), (3, "3")]
 67 
 68 // map(sequence, transformClosure): 如果transformClosure适用于所给序列中所有的元素,则返回一个新序列。
 69 
 70 // MARK: 6.flapMap函数
 71 let flapMap_ary  = [["B", "A", "C"],["1","5"]]
 72 
 73 //flapMap函数会降低维度
 74 //flapMap_ary.flatMap(<#T##transform: (Array<String>) throws -> ElementOfResult?##(Array<String>) throws -> ElementOfResult?#>)
 75 let flapMapAry = flapMap_ary.flatMap{$0}
 76 print(flapMapAry)
 77 //打印结果
 78 //["B", "A", "C", "1", "5"] // 二维数组变成了一维
 79 
 80 // MARK: 7.筛选函数filter
 81 let numbers = [1, 2, 3, 4, 5, 6]
 82 
 83 //获取偶数值
 84 let evens = numbers.filter{$0 % 2 == 0}
 85 print(evens)
 86 //打印结果
 87 //[2, 4, 6]
 88 
 89 // MARK: 8.reduce函数
 90 let arr_reduce = [1, 2, 4,]
 91 
 92 //对数组各元素求和
 93 let sum = arr_reduce.reduce(0) {$0 + $1}
 94 print(sum)
 95 //打印结果
 96 //7
 97 //arr_reduce.reduce(<#T##initialResult: Result##Result#>, <#T##nextPartialResult: (Result, Int) throws -> Result##(Result, Int) throws -> Result#>)
 98 //对数组各元素求积
 99 let product =  arr_reduce.reduce(1) {$0 * $1}
100 print(product)
101 //打印结果
102 //8
103 
104 // MARK: 9.dump(object): 一个对象的内容转储到标准输出
105 dump(arr_reduce)
106 
107 /*
108  
109  ▿ 3 elements
110  - 1
111  - 2
112  - 4
113  
114  */
115 // MARK: 10.indices(sequence): 在指定的序列中返回元素的索引(零索引)
116 for i in arr_reduce.indices {
117     print(i)
118     // 0 1 2
119 }
120 
121 // MARK: 11.一些在编程中经常会用到的函数
122 abs(-1) == 1 //获取绝对值
123 ["1","6","4"].contains("2") == false  //判断序列是否包含元素
124  ["a","b"].dropLast() == ["a"]  //剔除最后一个元素
125 ["a","b"].dropFirst() == ["b"] //剔除第一个元素
126 ["a","b"].elementsEqual(["b","a"]) == false  //判断序列是否相同
127  ["a","b"].indices == 0..<2  //获取index(indices是index的复数)
128 ["A", "B", "C"].joined(separator: ":") == "A:B:C" //将序列以分隔符串联起来成为字符串
129 Array([2, 7, 0].reversed()) == [0, 7, 2]   //逆序,注意返回的并非原类型序列
130 
131 // MARK: 常用的 74 个函数
132 /*
133  
134  abs(...)
135  advance(...)
136  alignof(...)
137  alignofValue(...)
138  assert(...)
139  bridgeFromObjectiveC(...)
140  bridgeFromObjectiveCUnconditional(...)
141  bridgeToObjectiveC(...)
142  bridgeToObjectiveCUnconditional(...)
143  c_malloc_size(...)
144  c_memcpy(...)
145  c_putchar(...)
146  contains(...)
147  count(...)
148  countElements(...)
149  countLeadingZeros(...)
150  debugPrint(...)
151  debugPrintln(...)
152  distance(...)
153  dropFirst(...)
154  dropLast(...)
155  dump(...)
156  encodeBitsAsWords(...)
157  enumerate(...)
158  equal(...)
159  filter(...)
160  find(...)
161  getBridgedObjectiveCType(...)
162  getVaList(...)
163  indices(...)
164  insertionSort(...)
165  isBridgedToObjectiveC(...)
166  isBridgedVerbatimToObjectiveC(...)
167  isUniquelyReferenced(...)
168  join(...)
169  lexicographicalCompare(...)
170  map(...)
171  max(...)
172  maxElement(...)
173  min(...)
174  minElement(...)
175  numericCast(...)
176  partition(...)
177  posix_read(...)
178  posix_write(...)
179  print(...)
180  println(...)
181  quickSort(...)
182  reduce(...)
183  reflect(...)
184  reinterpretCast(...)
185  reverse(...)
186  roundUpToAlignment(...)
187  sizeof(...)
188  sizeofValue(...)
189  sort(...)
190  split(...)
191  startsWith(...)
192  strideof(...)
193  strideofValue(...)
194  swap(...)
195  swift_MagicMirrorData_summaryImpl(...)
196  swift_bufferAllocate(...)
197  swift_keepAlive(...)
198  toString(...)
199  transcode(...)
200  underestimateCount(...)
201  unsafeReflect(...)
202  withExtendedLifetime(...)
203  withObjectAtPlusZero(...)
204  withUnsafePointer(...)
205  withUnsafePointerToObject(...)
206  withUnsafePointers(...) 
207  withVaList(...)
208  
209  */

 

posted @ 2017-09-07 15:40  路人Q  阅读(628)  评论(0编辑  收藏  举报