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.
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(...) 目前我知道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(...)
// assert mentioned on page 55
assert(true)
// countElements mentioned on page 79 原来是countElement现在是count
count("foo") ==
// enumerate mentioned 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(, , ) ==
// print mentioned on page 85
print("Hello ")
// println mentioned on page 4
println("World")
// sort mentioned on page 14
var a = ["B","A"]
sort(&a)
for i in a {
// "A", "B" will be printed
println(i)
}
abs(signedNumber): Returns the absolute value of a given signed number. Trivial but not documented.
abs(-) ==
abs(-) ==
abs() ==
contains(sequence, element): Returns true if a given sequence (such as an array) contains the specified element.
var languages = ["Swift", "Objective-C"]
contains(languages, "Swift") == true
contains(languages, "Java") == false
contains([, , , , ], ) == true
dropFirst(sequence): Returns a new sequence (such as an array) without the first element of the sequence.
languages = ["Swift", "Objective-C"]
var oldLanguages = dropFirst(languages)
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.
languages = ["Swift", "Objective-C"]
var newLanguages = dropLast(languages)
equal(newLanguages, ["Swift"]) == true
dump(object): Dumps the contents of an object to standard output.
languages = ["Swift", "Objective-C"]
dump(languages)
// Prints:
// ▿ 2 elements
// - [0]: Swift
// - [1]: Objective-C
equal(sequence1, sequence2): Returns true if sequence1 and sequence2 contain the same elements.
languages = ["Swift", "Objective-C"]
equal(languages, ["Swift", "Objective-C"]) == true
oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == true
filter(sequence, includeElementClosure): Returns a the elements from sequence that evaluate to true by includeElementClosure.
for i in filter(..., { $ % == }) {
// 10, 20, 30, ...
println(i)
assert(contains([, , , , , , , , , ], i))
}
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.
languages = ["Swift", "Objective-C"]
find(languages, "Objective-C") ==
find(languages, "Java") == nil
find([, , , , ], ) ==
indices(sequence): Returns the indices (zero indexed) of the elements in the given sequence.
equal(indices([, , ]), [, , ])
for i in indices([, , ]) {
// 0, 1, 2
println(i)
}
join(separator, sequence): Returns the elements of the supplied sequence separated by the given separator.
join(":", ["A", "B", "C"]) == "A:B:C"
languages = ["Swift", "Objective-C"]
join("/", languages) == "Swift/Objective-C"
map(sequence, transformClosure): Returns a new sequence with the transformClosure applied to all elements in the supplied sequence.
equal(map(..., { $ * }), [, , ])
for i in map(..., { $ * }) {
// 10, 20, 30, ...
println(i)
assert(contains([, , , , , , , , , ], i))
}
max(comparable1, comparable2, etc.): Returns the largest of the arguments given to the function.
max(, ) ==
max(, , ) ==
maxElement(sequence): Returns the largest element in a supplied sequence of comparable elements.
maxElement(...) ==
languages = ["Swift", "Objective-C"]
maxElement(languages) == "Swift"
minElements(sequence): Returns the smallest element in a supplied sequence of comparable elements.
minElement(...) ==
languages = ["Swift", "Objective-C"]
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.这个玩意一点都不懂。
languages = ["Swift", "Objective-C"]
reduce(languages, "", { $ + $ }) == "SwiftObjective-C"
reduce([, , ], , { $ * $ }) ==
reverse(sequence): Returns the elements of the given sequence reversed.
equal(reverse([, , ]), [, , ])
for i in reverse([, , ]) {
// 3, 2, 1
println(i)
}
startsWith(sequence1, sequence2): Return true if the starting elements sequence1 are equal to the of sequence2.
startsWith("foobar", "foo") == true
startsWith(..., ...) == true
languages = ["Swift", "Objective-C"]
startsWith(languages, ["Swift"]) == true
Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions的更多相关文章
- Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift
About Swift 关于Swift 官方文档的翻译,仅供参考,本人英语本就不好,边学边翻译,不喜勿喷. Swift is a new programming language for iOS, O ...
- 1 [main] DEBUG Sigar - no sigar-amd64-winnt.dll in java.library.path org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path
github上一个java项目,在myeclipse中运行正常,生成jar后,运行报错: 1 [main] DEBUG Sigar - no sigar-amd64-winnt.dll in java ...
- The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path 问题解决记录
1.问题 启动Tomcat之后,在浏览器输入IP后显示503,查看catalina.log发现报错: 2.问题定位:缺少 tomcat-native library 就是说 缺少Tomcat Nati ...
- 在Swift项目中使用OC,在OC项目中使用Swift
几天前,我开始新的App的开发了.终于有机会把swift用在实战中了,也学到了之前纯学语法时没有机会获得的知识. 这篇博文中,我就如何使用swift.OC混编做一个介绍. OC中使用Swift 首先, ...
- 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift
Swift 2.0学习笔记(Day4)——用Playground工具编写Swift 原创文章,欢迎转载.转载请注明:关东升的博客 用Playground编写Swift代码目的是为了学习.测试算法.验证 ...
- 《从零开始学Swift》学习笔记http(Day1)——我的第一行Swift代码
Swift 2.0学习笔记(Day1)——我的第一行Swift代码 原创文章,欢迎转载.转载请注明:关东升的博客 当第一次看到下面代码时我石化了,这些代码是什么东东?单词拼出来的? import Fo ...
- 【iOS】swift 74个Swift标准库函数
本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
随机推荐
- 20款优秀的国外 Mobile App 界面设计案例
在下面给大家分享的移动应用程序界面设计作品中,你可以看到不同创意类型的视觉效果.如果你想获得灵感,那很有必要看看下面20个优秀用户体验的移动应用 UI 设计.想要获取更多的灵感,可以访问移动开发分类, ...
- [数据库]sql之行顺序
这个文章主要是防止我忘了sql的执行顺序,解释的东西我都没怎么看懂.数据库渣如我- 逻辑查询处理阶段简介 FROM:对FROM子句中的前两个表执行笛卡尔积(Cartesian product)(交叉联 ...
- ASP.NET Core学习零散记录
赶着潮流听着歌,学着.net玩着Core 竹子学Core,目前主要看老A(http://www.cnblogs.com/artech/)和tom大叔的博客(http://www.cnblogs.com ...
- 学期总结ngu
不知不觉一年就过去了,真可谓光阴似箭,日月如梭,在这一年里,我成长了许多,懂得了如何跟队友合作,提高了我的交际能力,懂得了许多课本知识,增进了我的编写能力.最重要的是学会了总结经验,这无疑是我这一年里 ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
- C#反射的应用
项目框架中有一个很实用的方法,它用来获取客户端post的数据,并自动赋值到对象各属性,这样后台少写了很多代码.但是对于有主表.子表的表单,框架中没有提供自动给子表对象各属性赋值的方法,每次都要写很多代 ...
- Chrome弹窗的简单应用(选择结构与循环结构)
★选择结构★ ★JS实现弹窗显示随机数 示例代码效果图 ★ 弹窗实现对随机数的进一步判断 示例代码效果图 ★综合应用 比较大小 ★ 判断成绩等级 ): : : : : alert(" ...
- 百度地图js根据经纬度定位和拖动定位点
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 老毛桃安装Win8(哪里不会点哪里,so easy)
先来一张美女图,是不是很漂亮呢!继续往下看! 英雄不问出路,美女不看岁数!求推荐啊! 每次碰到妹子找我装系统的时候我都毫不犹豫的答应了,心里暗暗想到:好好表现啊!此刻的心情比见家长还要激动和紧张! 有 ...
- trie树---(插入、删除、查询字符串)
HDU 5687 Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字 ...