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的更多相关文章

  1. Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

    About Swift 关于Swift 官方文档的翻译,仅供参考,本人英语本就不好,边学边翻译,不喜勿喷. Swift is a new programming language for iOS, O ...

  2. 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 ...

  3. 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 ...

  4. 在Swift项目中使用OC,在OC项目中使用Swift

    几天前,我开始新的App的开发了.终于有机会把swift用在实战中了,也学到了之前纯学语法时没有机会获得的知识. 这篇博文中,我就如何使用swift.OC混编做一个介绍. OC中使用Swift 首先, ...

  5. 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift

    Swift 2.0学习笔记(Day4)——用Playground工具编写Swift 原创文章,欢迎转载.转载请注明:关东升的博客 用Playground编写Swift代码目的是为了学习.测试算法.验证 ...

  6. 《从零开始学Swift》学习笔记http(Day1)——我的第一行Swift代码

    Swift 2.0学习笔记(Day1)——我的第一行Swift代码 原创文章,欢迎转载.转载请注明:关东升的博客 当第一次看到下面代码时我石化了,这些代码是什么东东?单词拼出来的? import Fo ...

  7. 【iOS】swift 74个Swift标准库函数

    本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...

  8. 74个Swift标准库函数

    74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...

  9. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

随机推荐

  1. 初探KMP算法

            数据结构上老师也没讲这个,平常ACM比赛时我也没怎么理解,只是背会了代码--前天在博客园上看见了一篇介绍KMP的,不经意间就勾起了我的回忆,写下来吧,记得更牢. 一.理论准备      ...

  2. 二叉查找树(二)之 C++的实现

    概要 上一章介绍了"二叉查找树的相关理论知识,并通过C语言实现了二叉查找树".这一章给出二叉查找树的C++版本.这里不再对树的相关概念进行介绍,若遇到不明白的概念,可以在上一章查找 ...

  3. LoRaWAN协议(二)--LoRaWAN MAC数据包格式

    名词解析 上行:终端的数据发送经过一个或多个网关中转到达网络服务器. 下行:由网络服务器发送给终端设备,每条消息对应的终端设备是唯一确定的,而且只通过一个网关中转. LoRaWAN Classes L ...

  4. debian系统root用户登录

    Debian默认不允许root登录,所以修改之. 让Debian以root登录 1).首先修改gdm3的设定文件(/etc/gdm3/deamon.conf),在[security]字段后面追加如下一 ...

  5. [python]逆水行舟不进则退(1)

    工作后迎来的第一个长假期,打算在家休息一下,看看书之类的.但是不写点东西,不做点东西,感觉有些浪费时间.同时也想通过做点东西检验下自己这段时间的收获.其实在我开始写这篇文章的时候心里还是很没底的-交代 ...

  6. CentOS6.5菜鸟之旅:安装VirtualBox4.3

    一.下载VirtualBox的RHEL软件库配置文件 cd /etc/yum.repos.d wget http://download.virtualbox.org/virtualbox/rpm/rh ...

  7. Gradle学习系列之八——构建多个Project

    在本系列的上篇文章中,我们讲到了Gradle的依赖管理,在本篇文章中,我们将讲到如何构建多个Project. 请通过以下方式下载本系列文章的Github示例代码: git clone https:// ...

  8. struts2 s:if标签以及 #,%{},%{#}的使用方法

    <s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...

  9. 用Qt写软件系列三:一个简单的系统工具之界面美化

    前言 在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出.进程子模块信息等功能,留到后面再来慢慢实现.这一篇来讲述如何对主界面进行个性化的定制.Qt库提供的只是最基本的组件功能,使用这些组件开 ...

  10. Azure开发者任务之二:Cloud Service项目添加到ASP.Net Web中

    假设我们正在把现有的Web应用程序或ASP.Net MVC Web应用程序迁移到云中.在这种情况下,我们需要把云服务添加到现有的Web应用程序或ASP.Net MVC Web应用程序中. 我们有一个W ...