Swift中的74个内建函数

前端之家收集整理的这篇文章主要介绍了Swift中的74个内建函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
  1. abs(...)

  2. advance(...)

  3. alignof(...)

  4. alignofValue(...)

  5. assert(...)

  6. bridgeFromObjectiveC(...)

  7. bridgeFromObjectiveCUnconditional(...)

  8. bridgeToObjectiveC(...)

  9. bridgeToObjectiveCUnconditional(...)

  10. c_malloc_size(...)

  11. c_memcpy(...)

  12. c_putchar(...)

  13. contains(...)

  14. count(...)

  15. countElements(...)

  16. countLeadingZeros(...)

  17. debugPrint(...)

  18. debugPrintln(...)

  19. distance(...)

  20. dropFirst(...)

  21. dropLast(...)

  22. dump(...)

  23. encodeBitsAsWords(...)

  24. enumerate(...)

  25. equal(...)

  26. filter(...)

  27. find(...)

  28. getBridgedObjectiveCType(...)

  29. getVaList(...)

  30. indices(...)

  31. insertionSort(...)

  32. isBridgedToObjectiveC(...)

  33. isBridgedVerbatimToObjectiveC(...)

  34. isUniquelyReferenced(...)

  35. join(...)

  36. lexicographicalCompare(...)

  37. map(...)

  38. max(...)

  39. maxElement(...)

  40. min(...)

  41. minElement(...)

  42. numericCast(...)

  43. partition(...)

  44. posix_read(...)

  45. posix_write(...)

  46. print(...)

  47. println(...)

  48. quickSort(...)

  49. reduce(...)

  50. reflect(...)

  51. reinterpretCast(...)

  52. reverse(...)

  53. roundUpToAlignment(...)

  54. sizeof(...)

  55. sizeofValue(...)

  56. sort(...)

  57. split(...)

  58. startsWith(...)

  59. strideof(...)

  60. strideofValue(...)

  61. swap(...)

  62. swift_MagicMirrorData_summaryImpl(...)

  63. swift_bufferAllocate(...)

  64. swift_keepAlive(...)

  65. toString(...)

  66. transcode(...)

  67. underestimateCount(...)

  68. unsafeReflect(...)

  69. withExtendedLifetime(...)

  70. withObjectAtPlusZero(...)

  71. withUnsafePointer(...)

  72. withUnsafePointerToObject(...)

  73. withUnsafePointers(...)

  74. withVaList(...)

一些常用的:

abs(signedNumber): 返回给定的有符号数字的绝对值。很简单,但是没有在文档中记录。

abs(-1)==1abs(-42)==42abs(42)==42

contains(sequence,element): 如果给定的序列(如数组)包含特定的元素,则返回true。

varlanguages=["Swift","Objective-C"]contains(languages,"Swift")==truecontains(languages,"Java")==falsecontains([29,85,42,96,75],42)==true

dropFirst(sequence): 返回一个去掉第一个元素的新序列(如数组)。

varlanguages=["Swift","Objective-C"]varoldLanguages=dropFirst(languages)equal(oldLanguages,["Objective-C"])==true

dropLast(sequence): 返回一个的新序列(如数组),该序列去掉作为参数传递给函数的最后一个元素。

varlanguages=["Swift","Objective-C"]varnewLanguages=dropLast(languages)equal(newLanguages,["Swift"])==true

dump(object): 一个对象的内容转储到标准输出

varlanguages=["Swift","Objective-C"]dump(languages)//Prints://?2elements//-[0]:Swift//-[1]:Objective-C

equal(sequence1,sequence2): 如果序列1和序列2包含相同的元素,则返回true。

varlanguages=["Swift","Objective-C"]equal(languages,["Swift","Objective-C"])==truevaroldLanguages=dropFirst(languages)equal(oldLanguages,["Objective-C"])==true

filter(sequence,includeElementClosure): 返回序列的一个元素,这个元素满足includeElementClosure所指定的条件。

foriinfilter(1...100,{$0%10==0}){//10,20,30,...println(i)assert(contains([10,40,50,60,70,80,90,100],i))}

find(sequence,element): 再给定的序列中返回一个指定的索引,如果在序列中没有找到这个元素就返回nil。

varlanguages=["Swift","Objective-C"]find(languages,"Objective-C")==1find(languages,"Java")==nilfind([29,42)==2

indices(sequence): 在指定的序列中返回元素的索引(零索引)。

equal(indices([29,42]),[0,1,2])foriinindices([29,42]){//0,2println(i)}

join(separator,sequence): 返回一个由给定的分隔符分离出来的序列的元素。

join(":",["A","B","C"])=="A:B:C"varlanguages=["Swift","Objective-C"]join("/",languages)=="Swift/Objective-C"

map(sequence,transformClosure): 如果transformClosure适用于所给序列中所有的元素,则返回一个新序列。

equal(map(1...3,{$0*5}),[5,10,15])foriinmap(1...10,{$0*10}){//10,i))}

max(comparable1,comparable2,etc.): 返回函数所给参数中的最大值。

max(0,1)==1max(8,2,3)==8

maxElement(sequence): 返回所给序列的同类元素中的最大元素。

maxElement(1...10)==10varlanguages=["Swift","Objective-C"]maxElement(languages)=="Swift"

minElements(sequence): 返回所给序列的同类元素中的最小元素。

minElement(1...10)==1varlanguages=["Swift","Objective-C"]minElement(languages)=="Objective-C"

reduce(sequence,initial,combineClosure): 从第一个初始值开始对其进行combineClosure操作,递归式地将序列中的元素合并为一个元素。

varlanguages=["Swift","Objective-C"]reduce(languages,"",{$0+$1})=="SwiftObjective-C"reduce([10,5],{$0*$1})==1000

reverse(sequence): 返回所给序列的倒序。

equal(reverse([1,3]),[3,1])foriinreverse([1,3]){//3,1println(i)}

startsWith(sequence1,sequence2):如果序列1和序列2的起始元素相等,则返回true。

 
  1. startsWith("foobar","foo")==true

  2. startsWith(10..100,10..15)==true

  3. varlanguages=["Swift","Objective-C"]

  4. startsWith(languages,["Swift"])==true

猜你在找的Swift相关文章