Swift Standard Library

Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift Programming Language”). The rest remain undocumented.

This article lists all built-in Swift functions – both documented and undocumented ones. The definition used for “built-in function” used in this article is a function available in Swift withoutimporting any modules (such as Foundation, etc.) or referencing any classes.

Let’s start with the seven documented built-in functions mentioned in the Swift book along with the page number on which the function was first mentioned:

[objc]  view plain copy print ?
  1. // assert mentioned on page 55  
  2. assert(true)  
  3. // countElements mentioned on page 79  
  4. countElements("foo") == 3  
  5. // enumerate mentioned on page 94  
  6. for (i, j) in enumerate(["A""B"]) {  
  7.     // "0:A", "1:B" will be printed  
  8.     println("\(i):\(j)")  
  9. }  
  10. // min mentioned on page 246  
  11. min(823) == 2  
  12. // print mentioned on page 85  
  13. print("Hello ")  
  14. // println mentioned on page 4  
  15. println("World")  
  16. // sort mentioned on page 14  
  17. for i in sort(["B""A"]) {  
  18.     // "A", "B" will be printed  
  19.     println(i)  
  20. }  

Now on to the most useful undocumented functions …

abs(signedNumber): Returns the absolute value of a given signed number. Trivial but not documented.

[objc]  view plain copy print ?
  1. abs(-1) == 1  
  2. abs(-42) == 42  
  3. abs(42) == 42  

contains(sequence, element) : Returns true if a given sequence (such as an array) contains the specified element.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. contains(languages, "Swift") == true  
  3. contains(languages, "Java") == false  
  4. contains([29854296, 75], 42) == true  

dropFirst(sequence) : Returns a new sequence (such as an array) without the first element of the sequence.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. var oldLanguages = dropFirst(languages)  
  3. equal(oldLanguages, ["Objective-C"]) == true  

dropLast(sequence) : Returns a new sequence (such as an array) without the last element of the sequence passed as argument to the function.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. var newLanguages = dropLast(languages)  
  3. equal(newLanguages, ["Swift"]) == true  

dump(object): Dumps the contents of an object to standard output.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. dump(languages)  
  3. // Prints:  
  4. // ▿ 2 elements  
  5. //   - [0]: Swift  
  6. //   - [1]: Objective-C  

equal(sequence1, sequence2): Returns true if sequence1 and sequence2 contain the same elements.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. equal(languages, ["Swift""Objective-C"]) == true  
  3. var oldLanguages = dropFirst(languages)  
  4. equal(oldLanguages, ["Objective-C"]) == true  

filter(sequence, includeElementClosure) : Returns a the elements from sequence that evaluate to true by includeElementClosure.

[objc]  view plain copy print ?
  1. for i in filter(1...100, { $0 % 10 == 0 }) {  
  2.     // 10, 20, 30, ...  
  3.     println(i)  
  4.     assert(contains([102030405060708090, 100], i))  
  5. }  

find(sequence, element) : Return the index of a specified element in the given sequence. Or nil if the element is not found in the sequence.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. find(languages, "Objective-C") == 1  
  3. find(languages, "Java") == nil  
  4. find([29854296, 75], 42) == 2  

indices(sequence) : Returns the indices (zero indexed) of the elements in the given sequence.

[objc]  view plain copy print ?
  1. equal(indices([2985, 42]), [01, 2])  
  2. for i in indices([2985, 42]) {  
  3.     // 0, 1, 2  
  4.     println(i)  
  5. }  

join(separator, sequence) : Returns the elements of the supplied sequence separated by the given separator.

[objc]  view plain copy print ?
  1. join(":", ["A""B""C"]) == "A:B:C"  
  2. var languages = ["Swift""Objective-C"]  
  3. join("/", languages) == "Swift/Objective-C"  

map(sequence, transformClosure): Returns a new sequence with the transformClosure applied to all elements in the supplied sequence.

[objc]  view plain copy print ?
  1. equal(map(1...3, { $00 * 5 }), [510, 15])  
  2. for i in map(1...10, { $00 * 10 }) {  
  3.     // 10, 20, 30, ...  
  4.     println(i)  
  5.     assert(contains([102030405060708090, 100], i))  
  6. }  

max(comparable1, comparable2, etc.): Returns the largest of the arguments given to the function.

[objc]  view plain copy print ?
  1. max(01) == 1  
  2. max(823) == 8  

maxElement(sequence): Returns the largest element in a supplied sequence of comparable elements.

[objc]  view plain copy print ?
  1. maxElement(1...10) == 10  
  2. var languages = ["Swift""Objective-C"]  
  3. maxElement(languages) == "Swift"  

minElements(sequence) : Returns the smallest element in a supplied sequence of comparable elements.

[objc]  view plain copy print ?
  1. minElement(1...10) == 1  
  2. var languages = ["Swift""Objective-C"]  
  3. minElement(languages) == "Objective-C"  

reduce(sequence, initial, combineClosure): Recursively reduce the elements in sequence into one value by running the combineClosure on them with starting value of initial.

[objc]  view plain copy print ?
  1. var languages = ["Swift""Objective-C"]  
  2. reduce(languages, "", { $0 + $1 }) == "SwiftObjective-C"  
  3. reduce([1020, 5], 1, { $00 * $1 }) == 1000  

reverse(sequence): Returns the elements of the given sequence reversed.

[objc]  view plain copy print ?
  1. equal(reverse([12, 3]), [32, 1])  
  2. for i in reverse([12, 3]) {  
  3.     // 3, 2, 1  
  4.     println(i)  
  5. }  

startsWith(sequence1, sequence2): Return true if the starting elements sequence1 are equal to the of sequence2.

[objc]  view plain copy print ?
  1. startsWith("foobar""foo") == true  
  2. startsWith(10..10010..15) == true  
  3. var languages = ["Swift""Objective-C"]  
  4. startsWith(languages, ["Swift"]) == true  

Below is the full list of all 74 built-in functions in Swift. The functions covered above are the ones I think are useful on a day-to-day basis, but perhaps I’ve missed some functions from the list below that deserves coverage. If so, let me know in the comments section and please include a short code snippet to show how to use the function.

Happy Swifting!

[objc]  view plain copy print ?
  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(...)  

你可能感兴趣的:(function,Objective-C,swift)