Introduction to Swift's 74 commonly used built in functions

  • 2020-05-12 06:16:36
  • OfStack

Swift contains 74 built-in functions, but only seven of them are covered in the The Swift Programming Langage 1 book. The rest are not documented.

This article lists all the Swift library functions. Built-in functions in this article refer to functions that can be used directly without introducing any modules (e.g., Fundation, etc.).

Let's take a look at the seven library functions mentioned in the documentation:

Here are some useful library functions that are not documented:


// Assert if the parameter is `true` Then continue, otherwise throw an exception
//assert mentioned on page 55
assert(true)
 
// Count the number of elements in a sequence
// countElements mentioned on page 79
countElements("foo") == 3
 
// return 1 Three new sequences where each element is 1 A tuple,
// The first 1 Is the position of the original element `index` In the first 2 Are the elements 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)")
}
 
// Returns the minimum value of all parameters
// min mentioned on page 246
min(8, 2, 3) == 2
 
// print
// print mentioned on page 85
print("Hello ")
 
// Print (with line feed)
// println mentioned on page 4
println("World")
 
// The sorting
// 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 (say, 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) : prints 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) : 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"]) == true

filter(sequence, includeElementClosure) : executes the includeElementClosure closure on each element in the sequence sequence, and synthesizes all elements whose closure result is true 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 index to the location 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 position of all elements in the sequence sequence (indices is the plural 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) : concatenates the sequence sequence into a string via the delimiter separator and returns the string.

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

map(sequence, transformClosure) : executes the includeElementClosure closure on 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 initial and the first element in the sequence are passed into combineClosure as parameters for operation, and the result is saved to initial; Then, initial and the second element are passed into combineClosure for calculation, and the result is saved to initial. Double count until all elements in sequence have been calculated and return the final initial value.


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

reverse(sequence) : returns the reverse sequence 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 beginning element in the sequence sequence1 is the same as all the elements in the sequence sequence2, otherwise returns 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 the ones I think will be used a lot in Swift programming.

Complete 74 built-in functions:


abs(-1) == 1
abs(-42) == 42
abs(42) == 42
8


Related articles: