Swift has 74 built-in functions

Source: Internet
Author: User

Swift has 74 built-in functions, but in Swift's official documentation ("The Swift programming Language") was recorded only in 7. The remaining 67 are not recorded. This article lists all of the built-in functions of Swift. The so-called built-in functions mentioned in this article refer to functions that do not need to import any modules (such as foundation, etc.) or reference any class in Swift. ABS (Signednumber): Returns the absolute value of the given signed number. Very simple, but not documented in the document.
    1. ABS (-1) = = 1
    2. ABS (-42) = = 42
    3. ABS (42) = = 42
contains (sequence, Element): Returns True if the given sequence (such as an array) contains a specific element.
    1. var languages = ["Swift", "Objective-c"]
    2. Contains (languages, "Swift") = = true
    3. Contains (languages, "Java") = = false
    4. Contains ([+], (+), +-----) = = true
Dropfirst (Sequence): Returns a new sequence (such as an array) that removes the first element.
    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) that removes the last element passed as a parameter to the function.
    1. var languages = ["Swift", "Objective-c"]
    2. var newlanguages = Droplast (Languages)
    3. Equal (Newlanguages, ["Swift"]) = = true
dump (object): The contents of an object are dumped to standard output.
    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 sequence 1 and sequence 2 contain the same element.
    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 an element of the sequence that satisfies the conditions specified by includeelementclosure.
    1. For I in filter (1...100, {$ 0)
    2. {
    3. //Ten , ...
    4. println (i)
    5. ASSERT (Contains ([Ten, J, Max, Max, Max, Max, +, +, +, +], i))
    6. }
find (sequence, Element): Returns a specified index in the given sequence, or nil if the element is not found in the sequence.
    1. var languages = ["Swift", "Objective-c"]
    2. Find (Languages, "objective-c") = = 1
    3. Find (Languages, "Java") = = Nil
    4. Find ([29, 85, 42, 96, 75], 42) = = 2
Indices (Sequence): Returns the index of the element in the specified sequence (0 index).
    1. Equal (indices ([29, 85, 42]), [0, 1, 2])
    2. For I in indices ([+,-]) {
    3. //0, 1, 2
    4. println (i)
    5. }
Join (separator, sequence): Returns an element of a sequence separated by a given delimiter.
    1. Join (":", ["A", "B", "C"]) = = "A:b:c"
    2. var languages = ["Swift", "Objective-c"]
    3. Join ("/", languages) = = "Swift/objective-c"
map (sequence, transformclosure): If transformclosure applies to all elements in the given sequence, a new sequence is returned.
    1. Equal (map (1...3, {$ * 5}), [5, 10, 15])
    2. For I in map (1...10, {$ *)} {
    3. //Ten , ...
    4. println (i)
    5. ASSERT (Contains ([Ten, J, Max, Max, Max, Max, +, +, +, +], i))
    6. }
Max (Comparable1, Comparable2, etc.): Returns the maximum value in the parameter given by the function.
    1. Max (0, 1) = = 1
    2. Max (8, 2, 3) = = 8
maxelement (Sequence): Returns the largest element in the same element of the given sequence.
    1. Maxelement (1...10) = = 10
    2. var languages = ["Swift", "Objective-c"]
    3. Maxelement (languages) = = "Swift"
minelements (Sequence): Returns the smallest element in the same element of the given sequence.
    1. Minelement (1...10) = = 1
    2. var languages = ["Swift", "Objective-c"]
    3. Minelement (languages) = = "Objective-c"
Reduce (sequence, initial, combineclosure): Combineclosure The first initial value, recursively merging the elements of the sequence into one element.
    1. var languages = ["Swift", "Objective-c"]
    2. Reduce (languages, " ", {$ + $}) = = "Swiftobjective-c"
    3. Reduce ([10, 20, 5], 1, {$ *}) = = 1000
reverse (Sequence): Returns the reverse of the given sequence.
    1. Equal (reverse ([1, 2, 3]), [3, 2, 1])
    2. For I in reverse ([1, 2, 3]) {
    3. //3, 2, 1
    4. println (i)
    5. }
StartsWith (Sequence1, Sequence2): Returns True if the starting element of sequence 1 and sequence 2 is equal.
    1. StartsWith ("Foobar", "foo") = = true
    2. StartsWith (10..100, 10..15) = = true
    3. var languages = ["Swift", "Objective-c"]
    4. StartsWith (Languages, ["Swift"]) = = true
The following list is the 74 built-in functions in Swift. The function described above is something I think is useful in everyday development. Of course I may have omitted some of the usual functions. You are welcome to discuss this in the comments, and please use a short code snippet to show how to use the function.
  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 (...)

Welcome to the discussion, please errata!

Swift has 74 built-in functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.