Swift contains 74 built-in functions, but only 7 of them are described in The Swift Programming Langage, and The rest are not documented.

This article lists all the Swift library functions. Built-in functions are those that can be used directly without introducing any modules (such as Fundation).

Here’s a look at seven library functions mentioned in the documentation:

Here are some library functions that are useful but not documented:

// assert that if the argument is' true 'then continue, CountElements ements on Page 79 countElements("foo") countElements on page 79 countElements("foo") == 3 // Return a new sequence, where each element is a tuple, // the first value is the position of the original element 'index', // Enumerate erate on page 94 for (I, j) in enumerate(["A", "B"]) {// "0:A", '1:B' will be printed println(' \(I):\(j) ')} // Min mentioned on page 246 min(8, 2, 1) 3) == 2 // println mentioned on page 85 print("Hello ") // println mentioned on page 4 println("World") // Sort on Page 14 for I in sort(["B", "A"]) {// "A", "B" will be printed println(I)}Copy the code

Abs (signedNumber) : returns the absolute value of a number

abs(-1) == 1
abs(-42) == 42
abs(42) == 42Copy the code

Contains (sequence, Element) : Returns true if a sequence sequence (such as an array) contains the specified element element, false otherwise.

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

DropFirst (sequence) : Returns a new sequence (such as a new array) with the first element removed.


var languages = ["Swift", "Objective-C"]
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == trueCopy the code

DropLast (sequence) : Returns a new sequence (such as a new array) with the last element removed.



var languages = ["Swift", "Objective-C"]
var newLanguages = dropLast(languages)
equal(newLanguages, ["Swift"]) == trueCopy the code

Dump (object) : Prints all information about an object


Var languages = ["Swift", "objective-c "] dump(languages) // Prints: // ▿ 2 elements // - [0]: Swift // - [1]: Objective-CCopy the code

Equal (sequence1, sequence2) : Determines whether two sequences are equal


var languages = ["Swift", "Objective-C"]
equal(languages, ["Swift", "Objective-C"]) == true
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == trueCopy the code

Filter (sequence, includeElementClosure) : Execute the includeElementClosure closure for each element in the sequence sequence and return a new sequence sequence sequence with all elements whose closure results in true.


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

Find (sequence, Element) : Returns the index of an element in the sequence sequence. If the element does not exist in the sequence, nil is returned.

var languages = ["Swift", "Objective-C"]
find(languages, "Objective-C") == 1
find(languages, "Java") == nil
find([29, 85, 42, 96, 75], 42) == 2Copy the code

Indices (sequence) : Returns the positions of all elements in the sequence sequence (indices are the plural of indices)

equal(indices([29, 85, 42]), [0, 1, 2])
for i in indices([29, 85, 42]) {
 // 0, 1, 2
 println(i)
}Copy the code

Join (separator, sequence) : Concatenates the sequence sequence into a string using the separator and returns the string.


join(":", ["A", "B", "C"]) == "A:B:C"
var languages = ["Swift", "Objective-C"]
join("/", languages) == "Swift/Objective-C"Copy the code

Map (Sequence, transformClosure) : Executes an includeElementClosure closure for each element in a sequence sequence and returns the results of all closures as a new sequence sequence.

equal(map(1... 3, { $0 * 5 }), [5, 10, 15]) for i in map(1... {$0 * 10}) {// 10, 20, 30... println(i) assert(contains([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], i)) }Copy the code

Max (comparable1, comparable2, etc.) : Returns the maximum value of the parameter.


max(0, 1) == 1
max(8, 2, 3) == 8Copy the code

MaxElement (sequence) : Returns the maximum value in the sequence sequence.


maxElement(1... 10) == 10 var languages = ["Swift", "Objective-C"] maxElement(languages) == "Swift"Copy the code

MinElements (sequence) : Returns the minimum value in the sequence sequence.


minElement(1... 10) == 1 var languages = ["Swift", "Objective-C"] minElement(languages) == "Objective-C"Copy the code

Reduce (sequence, initial combineClosure) : Given a sequence sequence sequence and an initial value initial, then initial and the first element in the sequence are passed into combineClosure as parameters for operation, and the result obtained is saved to Initial; Then initial and the second element are passed into combineClosure to calculate, and the result is saved to Initial. Repeat the calculation until all elements in the sequence have been evaluated and the final initial value is returned.

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

Reverse (sequence) : Returns the sequence sequence in reverse order.

equal(reverse([1, 2, 3]), [3, 2, 1])
for i in reverse([1, 2, 3]) {
 // 3, 2, 1
 println(i)
}Copy the code

StartsWith (Sequence1, sequence2) : Returns true if the beginning element in sequence1 is equal to all elements in sequence2, false otherwise.

startsWith("foobar", "foo") == true startsWith(10.. 100, 10.. 15) == true var languages = ["Swift", "Objective-C"] startsWith(languages, ["Swift"]) == trueCopy the code

The functions mentioned above are the ones I think will be used a lot in Swift programming.

Complete 74 built-in functions:

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(...)Copy the code