在Swift编程中会经常用到的函数

Swift包含了74个内置函数,但在The Swift Programming Langage一书中只介绍了其中的7个,其它的都没有在文档中体现。

这篇文章列举出了所有的内置Swift函数。文中所谓的内置函数是指无需引入任何模块(比如说Fundation等)即可以直接使用的函数。

下面先来看看7个在文档中提到的内置函数:

//断言,参数如果为`true`则继续,否则抛出异常
// assert mentioned on page 55
assert(true)

//计算序列的元素个数
// countElements mentioned on page 79
countElements("foo") == 3

//返回一个新的序列,其中每个元素是一个元组,第一个值为原来元素所在的位置`index`,第二个为原来序列中的元素
// 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(8, 2, 3) == 2

//打印
// print mentioned on page 85
print("Hello ")

//打印(带换行)
// println mentioned on page 4
println("World")

//排序
// sort mentioned on page 14
for i in sort(["B", "A"]) {
// "A", "B" will be printed
println(i)
}

//返回数字的绝对值
abs(signedNumber)
abs(-1) == 1
abs(-42) == 42
abs(42) == 42

//如果某个序列sequence(比如说一个数组)包含指定的元素element,则返回true,否则返回false。
contains(sequence, element):
var languages = ["Swift", "Objective-C"]
contains(languages, "Swift") == true
contains(languages, "Java") == false
contains([29, 85, 42, 96, 75], 42) == true

//返回一个去掉了首个元素的、新的序列(比如一个新数组)。
dropFirst(sequence)
var languages = ["Swift", "Objective-C"]
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == true

//返回一个去掉了最后一个元素的、新的序列(比如一个新数组)。
dropLast(sequence)
var languages = ["Swift", "Objective-C"]
var newLanguages = dropLast(languages)
equal(newLanguages, ["Swift"]) == true

//打印出某个对象object的所有信息
dump(object)
var languages = ["Swift", "Objective-C"]
dump(languages)
// Prints:
// 1 2 elements
// - [0]: Swift
// - [1]: Objective-C

//判断两个序列是否相等
equal(sequence1, sequence2)
var languages = ["Swift", "Objective-C"]
equal(languages, ["Swift", "Objective-C"]) == true
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == true

//对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包结果为true的元素合成一个新序列sequence并返回。
filter(sequence, includeElementClosure)
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))
}

//返回序列sequence中某元素element的位置index。如果序列中不存在此元素,则返回nil。
find(sequence, element)
var languages = ["Swift", "Objective-C"]
find(languages, "Objective-C") == 1
find(languages, "Java") == nil
find([29, 85, 42, 96, 75], 42) == 2

//返回序列sequence中所有元素的位置(indices是index的复数)
indices(sequence)
equal(indices([29, 85, 42]), [0, 1, 2])
for i in indices([29, 85, 42]) {
// 0, 1, 2
println(i)
}

//将序列sequence通过分隔符separator连成一个字符串,并返回此字符串。
join(separator, sequence)
join(":", ["A", "B", "C"]) == "A:B:C"
var languages = ["Swift", "Objective-C"]
join("/", languages) == "Swift/Objective-C"

//对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包的结果合成一个新序列sequence并返回。
map(sequence, transformClosure)
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.)
max(0, 1) == 1
max(8, 2, 3) == 8

//返回序列sequence中的最大值。
maxElement(sequence)
maxElement(1...10) == 10
var languages = ["Swift", "Objective-C"]
maxElement(languages) == "Swift"

//返回序列sequence中的最小值。
minElement(sequence)
minElement(1...10) == 1
var languages = ["Swift", "Objective-C"]
minElement(languages) == "Objective-C"

//给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值。
reduce(sequence, initial, combineClosure)
var languages = ["Swift", "Objective-C"]
reduce(languages, "", { $0 + $1 }) == "SwiftObjective-C"
reduce([10, 20, 5], 1, { $0 * $1 }) == 1000

//返回逆序的序列sequence。
reverse(sequence)
equal(reverse([1, 2, 3]), [3, 2, 1])
for i in reverse([1, 2, 3]) {
// 3, 2, 1
println(i)
}

//如果序列sequence1中开头的元素跟序列sequence2中的所有元素都相等,则返回true,否则返回false。
startsWith(sequence1, sequence2)
startsWith("foobar", "foo") == true
startsWith(10..100, 10..15) == true
var languages = ["Swift", "Objective-C"]
startsWith(languages, ["Swift"]) == true

上面提到的函数是我认为在Swift编程中会经常用到的函数。下面将列出完整的74个函数列表。

完整74个内置函数:

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(...)
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(...)

Leave a Reply