Swift contains 74 built-in functions, but only 7 of them are introduced in the book The Swift Programming Langage, and the others are not reflected in the documentation.
This article lists all Swift library functions. The so-called built-in functions in the article refer to functions that can be used directly without introducing any module (such as Fundation, etc.).
Let’s first look at the 7 library functions mentioned in the documentation:
Here are some useful library functions that are not reflected in the documentation:
//Assert that if the parameter is `true`, continue, otherwise an exception will be thrown
//assert mentioned on page 55
assert(true)
//Calculate the number of elements in the sequence
// countElements mentioned on page 79
countElements("foo") == 3
//Return a new sequence where each element is a tuple,
//The first value is the original element's position `index`, and the second is the element in the original sequence
// enumerate mentioned on page 94
for (i, j) in enumerate(["A", "B"]) {
// "0:A", "1:B" will be printed
println("\(i):\(j)")
}
//Return the minimum value of all parameters
// min mentioned on page 246
min(8, 2, 3) == 2
// print mentioned on page 85
print("Hello ")
//Print (with line break)
// println mentioned on page 4
println("World")
//Sort
// sort mentioned on page 14
for i in sort(["B", "A"]) {
// "A", "B" will be printed
println(i)
}
abs(signedNumber): Returns the absolute value of the number
abs(-1) == 1
abs(-42) == 42
abs(42) == 42
contains(sequence, element): Returns true if a sequence sequence (for example, an array) contains the specified element element, otherwise returns false.
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"]) == true
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"]) == true
dump(object): Print out all information about an object object
var languages = ["Swift", "Objective-C"]
dump(languages)
// Prints:
// ▿ 2 elements
// - [0]: Swift
// - [1]: Objective-C
equal(sequence1, sequence2): determine whether two sequences are equal
var languages = ["Swift", "Objective-C"]
equal(languages, ["Swift", "Objective-C"]) == true
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == true
filter(sequence, includeElementClosure): executes an includeElementClosure closure on each element in the sequence sequence, and synthesizes all elements with true closure result into a new sequence sequence and returns.
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))
}
find(sequence, element): Returns the position index of an element element in the sequence sequence. If this 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) == 2
indices(sequence): Returns the positions of all elements in the sequence sequence (indices are plurals of index)
equal(indices([29, 85, 42]), [0, 1, 2])
for i in indices([29, 85, 42]) {
// 0, 1, 2
println(i)
}
join(separator, sequence): connect the sequence sequence into a string through the separator separator and return this string.
join(":", ["A", "B", "C"]) == "A:B:C"
var languages = ["Swift", "Objective-C"]
join("/", languages) == "Swift/Objective-C"
map(sequence, transformClosure): executes an includeElementClosure closure for each element in the sequence sequence, and synthesizes the results of all closures into a new sequence sequence and returns.
equal(map(1...3, { $0 * 5 }), [5, 10, 15])
for i in map(1...10, { $0 * 10 }) {
// 10, 20, 30, ...
println(i)
assert(contains([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], i))
}
max(comparable1, comparable2, etc.): Returns the maximum value in the parameter.
max(0, 1) == 1
max(8, 2, 3) == 8
maxElement(sequence): Returns the maximum value in the sequence sequence.
maxElement(1...10) == 10
var languages = ["Swift", "Objective-C"]
maxElement(languages) == "Swift"
minElements(sequence): Returns the minimum value in the sequence sequence.
minElement(1...10) == 1
var languages = ["Swift", "Objective-C"]
minElement(languages) == "Objective-C"
reduce(sequence, initial, combineClosure): Given a sequence sequence and an initial value initial, then pass initial and the first element in the sequence as parameters into combineClosure for calculation, and the result is saved to initial; then pass initial and second elements into combineClosure for calculation, and the result is saved to initial; repeat the calculation until all elements in the sequence are calculated 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 }) == 1000
reverse(sequence): Returns the sequence sequence of reverse order.
equal(reverse([1, 2, 3]), [3, 2, 1])
for i in reverse([1, 2, 3]) {
// 3, 2, 1
println(i)
}
startsWith(sequence1, sequence2): Return true if the element at the beginning of sequence sequence1 is equal to all elements in sequence sequence2, otherwise false.
startsWith("foobar", "foo") == true
startsWith(10..100, 10..15) == true
var languages = ["Swift", "Objective-C"]
startsWith(languages, ["Swift"]) == true
The functions mentioned above are functions that I think are often used 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(...)