Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions

简介:

Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift Programming Language”). The rest remain undocumented. This article lists all built-in Swift functions – both documented and undocumented ones. The definition used for “built-in function” used in this article is a function available in Swift without importing any modules (such as Foundation, etc.) or referencing any classes. Let’s start with the seven documented built-in functions mentioned in the Swift book along with the page number on which the function was first mentioned:
Below is the full list of all 74 built-in functions in Swift. The functions covered above are the ones I think are useful on a day-to-day basis, but perhaps I’ve missed some functions from the list below that deserves coverage. If so, let me know in the comments section and please include 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(...) 目前我知道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(...)
复制代码 复制代码
 1 // assert mentioned on page 55  2 assert(true)
 3 // countElements mentioned on page 79 原来是countElement现在是count  4 count("foo") == 3  5 // enumerate mentioned on page 94  6 for (i, j) in enumerate(["A", "B"]) {
 7 // "0:A", "1:B" will be printed  8 println("\(i):\(j)")
 9 }
10 // min mentioned on page 246 11 min(8, 2, 3) == 2 12 // print mentioned on page 85 13 print("Hello ")
14 // println mentioned on page 4 15 println("World")
16 // sort mentioned on page 14 17 var a = ["B","A"]
18 sort(&a)
19 for i in a {
20 // "A", "B" will be printed 21  println(i)
22 }
复制代码

abs(signedNumber): Returns the absolute value of a given signed number. Trivial but not documented.

1 abs(-1) == 1 2 abs(-42) == 42 3 abs(42) == 42

contains(sequence, element): Returns true if a given sequence (such as an array) contains the specified element.

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

dropFirst(sequence): Returns a new sequence (such as an array) without the first element of the sequence.

1 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) without the last element of the sequence passed as argument to the function.

1 languages = ["Swift", "Objective-C"]
2 var newLanguages = dropLast(languages)
3 equal(newLanguages, ["Swift"]) == true

dump(object): Dumps the contents of an object to standard output.

1 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 sequence1 and sequence2 contain the same elements.

1 languages = ["Swift", "Objective-C"]
2 equal(languages, ["Swift", "Objective-C"]) == true 3 oldLanguages = dropFirst(languages)
4 equal(oldLanguages, ["Objective-C"]) == true

filter(sequence, includeElementClosure): Returns a the elements from sequence that evaluate to true by includeElementClosure.

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

find(sequence, element): Return the index of a specified element in the given sequence. Or nil if the element is not found in the sequence.

1 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 indices (zero indexed) of the elements in the given sequence.

1 equal(indices([29, 85, 42]), [0, 1, 2])
2 for i in indices([29, 85, 42]) {
3 // 0, 1, 2 4  println(i)
5 }

join(separator, sequence): Returns the elements of the supplied sequence separated by the given separator.

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

map(sequence, transformClosure): Returns a new sequence with the transformClosure applied to all elements in the supplied sequence.

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

max(comparable1, comparable2, etc.): Returns the largest of the arguments given to the function.

1 max(0, 1) == 1 2 max(8, 2, 3) == 8

maxElement(sequence): Returns the largest element in a supplied sequence of comparable elements.

1 maxElement(1...10) == 10 2 languages = ["Swift", "Objective-C"]
3 maxElement(languages) == "Swift"

minElements(sequence): Returns the smallest element in a supplied sequence of comparable elements.

1 minElement(1...10) == 1 2 languages = ["Swift", "Objective-C"]
3 minElement(languages) == "Objective-C"

reduce(sequence, initial, combineClosure): Recursively reduce the elements in sequence into one value by running the combineClosure on them with starting value of initial.这个玩意一点都不懂。

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

reverse(sequence): Returns the elements of the given sequence reversed.

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): Return true if the starting elements sequence1 are equal to the of sequence2.

1 startsWith("foobar", "foo") == true 2 startsWith(1...100, 1...15) == true 3 languages = ["Swift", "Objective-C"]
4 startsWith(languages, ["Swift"]) == true

相关文章
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
监控 Swift iOS开发
局域网计算机监控软件中利用Swift构建iOS端的移动监控应用
在局域网计算机监控软件的开发中,构建iOS端的移动监控应用是一项关键任务。本文将介绍如何利用Swift语言实现这一目标,通过多个代码示例展示关键功能的实现。
223 1
|
3月前
|
安全 JavaScript 前端开发
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
65 0
|
5月前
|
JavaScript 前端开发 PHP
用swift开发ios移动端app应用初体验
直接跟着 apple 官方的 SwiftUI 教程跑的,写惯了 javascript 奔放的代码,很多语法理解起来还是有点费劲
57 1
|
7月前
|
Swift iOS开发
iOS OC混编Swift 后者无法走断点
iOS OC混编Swift 后者无法走断点
49 0
|
12月前
|
Swift iOS开发
IOS使用Swift加载Xib文件
IOS使用Swift加载Xib文件
241 0
|
Swift iOS开发
iOS开发 - 适合工程多环境切换(swift环境),适合经常打很多不同环境包的人
iOS开发 - 适合工程多环境切换(swift环境),适合经常打很多不同环境包的人
219 0
iOS开发 - 适合工程多环境切换(swift环境),适合经常打很多不同环境包的人
|
Swift 数据安全/隐私保护 iOS开发
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
342 0
iOS开发 - swift通过Alamofire实现https通信

相关课程

更多