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.
- ABS (-1) = = 1
- ABS (-42) = = 42
- ABS (42) = = 42
contains (sequence, Element): Returns True if the given sequence (such as an array) contains a specific element.
- var languages = ["Swift", "Objective-c"]
- Contains (languages, "Swift") = = true
- Contains (languages, "Java") = = false
- Contains ([+], (+), +-----) = = true
Dropfirst (Sequence): Returns a new sequence (such as an array) that removes the first element.
- var languages = ["Swift", "Objective-c"]
- var oldlanguages = Dropfirst (Languages)
- 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.
- var languages = ["Swift", "Objective-c"]
- var newlanguages = Droplast (Languages)
- Equal (Newlanguages, ["Swift"]) = = true
dump (object): The contents of an object are dumped to standard output.
- var languages = ["Swift", "Objective-c"]
- Dump (Languages)
- Prints:
- // ? 2 elements
- -[0]: Swift
- -[1]: Objective-c
equal (Sequence1, Sequence2): Returns True if sequence 1 and sequence 2 contain the same element.
- var languages = ["Swift", "Objective-c"]
- Equal (languages, ["Swift", "objective-c"]) = = true
- var oldlanguages = Dropfirst (Languages)
- Equal (Oldlanguages, ["objective-c"]) = = true
filter (sequence, includeelementclosure): Returns an element of the sequence that satisfies the conditions specified by includeelementclosure.
- For I in filter (1...100, {$ 0)
- {
- //Ten , ...
- println (i)
- ASSERT (Contains ([Ten, J, Max, Max, Max, Max, +, +, +, +], i))
- }
find (sequence, Element): Returns a specified index in the given sequence, or nil if the element is not found in the sequence.
- var languages = ["Swift", "Objective-c"]
- Find (Languages, "objective-c") = = 1
- Find (Languages, "Java") = = Nil
- Find ([29, 85, 42, 96, 75], 42) = = 2
Indices (Sequence): Returns the index of the element in the specified sequence (0 index).
- Equal (indices ([29, 85, 42]), [0, 1, 2])
- For I in indices ([+,-]) {
- //0, 1, 2
- println (i)
- }
Join (separator, sequence): Returns an element of a sequence separated by a given delimiter.
- Join (":", ["A", "B", "C"]) = = "A:b:c"
- var languages = ["Swift", "Objective-c"]
- Join ("/", languages) = = "Swift/objective-c"
map (sequence, transformclosure): If transformclosure applies to all elements in the given sequence, a new sequence is returned.
- Equal (map (1...3, {$ * 5}), [5, 10, 15])
- For I in map (1...10, {$ *)} {
- //Ten , ...
- println (i)
- ASSERT (Contains ([Ten, J, Max, Max, Max, Max, +, +, +, +], i))
- }
Max (Comparable1, Comparable2, etc.): Returns the maximum value in the parameter given by the function.
- Max (0, 1) = = 1
- Max (8, 2, 3) = = 8
maxelement (Sequence): Returns the largest element in the same element of the given sequence.
- Maxelement (1...10) = = 10
- var languages = ["Swift", "Objective-c"]
- Maxelement (languages) = = "Swift"
minelements (Sequence): Returns the smallest element in the same element of the given sequence.
- Minelement (1...10) = = 1
- var languages = ["Swift", "Objective-c"]
- Minelement (languages) = = "Objective-c"
Reduce (sequence, initial, combineclosure): Combineclosure The first initial value, recursively merging the elements of the sequence into one element.
- var languages = ["Swift", "Objective-c"]
- Reduce (languages, " ", {$ + $}) = = "Swiftobjective-c"
- Reduce ([10, 20, 5], 1, {$ *}) = = 1000
reverse (Sequence): Returns the reverse of the given sequence.
- Equal (reverse ([1, 2, 3]), [3, 2, 1])
- For I in reverse ([1, 2, 3]) {
- //3, 2, 1
- println (i)
- }
StartsWith (Sequence1, Sequence2): Returns True if the starting element of sequence 1 and sequence 2 is equal.
- StartsWith ("Foobar", "foo") = = true
- StartsWith (10..100, 10..15) = = true
- var languages = ["Swift", "Objective-c"]
- 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.
- ABS (...)
- Advance (...)
- Alignof (...)
- Alignofvalue (...)
- ASSERT (...)
- Bridgefromobjectivec (...)
- Bridgefromobjectivecunconditional (...)
- Bridgetoobjectivec (...)
- Bridgetoobjectivecunconditional (...)
- C_malloc_size (...)
- c_memcpy (...)
- C_putchar (...)
- Contains (...)
- Count (...)
- Countelements (...)
- Countleadingzeros (...)
- DebugPrint (...)
- Debugprintln (...)
- Distance (...)
- Dropfirst (...)
- Droplast (...)
- Dump (...)
- Encodebitsaswords (...)
- Enumerate (...)
- Equal (...)
- Filter (...)
- Find (...)
- Getbridgedobjectivectype (...)
- Getvalist (...)
- Indices (...)
- Insertionsort (...)
- Isbridgedtoobjectivec (...)
- Isbridgedverbatimtoobjectivec (...)
- Isuniquelyreferenced (...)
- Join (...)
- Lexicographicalcompare (...)
- Map (...)
- Max (...)
- Maxelement (...)
- Min (...)
- Minelement (...)
- Numericcast (...)
- Partition (...)
- Posix_read (...)
- Posix_write (...)
- Print (...)
- println (...)
- QuickSort (...)
- Reduce (...)
- Reflect (...)
- Reinterpretcast (...)
- Reverse (...)
- Rounduptoalignment (...)
- sizeof (...)
- Sizeofvalue (...)
- Sort (...)
- Split (...)
- StartsWith (...)
- Strideof (...)
- Strideofvalue (...)
- Swap (...)
- Swift_magicmirrordata_summaryimpl (...)
- Swift_bufferallocate (...)
- Swift_keepalive (...)
- ToString (...)
- Transcode (...)
- Underestimatecount (...)
- Unsafereflect (...)
- Withextendedlifetime (...)
- Withobjectatpluszero (...)
- Withunsafepointer (...)
- Withunsafepointertoobject (...)
- Withunsafepointers (...)
- Withvalist (...)
Welcome to the discussion, please errata!
Swift has 74 built-in functions