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
相关文章
- HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
- PHP二维数组排序(list_order)
- python中列表(list)函数及使用
- [Algorithm] 234. Palindrome Linked List / Reverse linked list
- [Python] Understand List Comprehensions in Python
- [Python] List & Object spread in Python
- [Redux] React Todo List Example (Adding a Todo)
- mysql 8.0下的SELECT list is not in GROUP BY clause and contains nonaggregated column
- mysql问题解决SELECT list is not in GROUP BY clause and contains nonaggregated column
- [LeetCode] Delete Node in a Linked List
- Column '表名.某列名' is invalid in the select list because it is not contained in either an aggregate f
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- how does SAP UI5 filter for list work in xml view
- How where used list in SAP GUI is implemented
- Item category dropdown list的determine - filtering 逻辑
- How to identify the product settype and attribute in a given list
- Column '表名.某列名' is invalid in the select list because it is not contained in either an aggregate f
- Atitit.attilax的 case list 项目经验 案例列表
- Reuse library debug in Chrome - phase3 exchange drop down list
- List集合之LinkedList(一)通过源码看特性
- Matlab:成功解决In an assignment A(I)=B,the number of elements in B and I must be the same
- Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘***_id‘ in ‘field list‘
- bash: fc-list: command not found,file: file:///etc/yum.repos.d/rhel-debuginfo.repo, line: 1‘se]n‘
- 【mysql】In aggregated query without GROUP BY, expression #1 of SELECT list contains ......
- List原理
- pyspark 通过list 构建rdd
- Mybatis多表查询,报错:Column 'id' in field list is ambiguous
- 【编程技巧】Stream流之list转map、分组取每组第一条
- MySQL ---- In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated