Swift的74个常用内置函数介绍及使用方法

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

contains(sequence, element):如果某个序列sequence(比如说一个数组)包含指定的元素element,则返回true,否则返回false。

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

dump(object):打印出某个对象object的所有信息

var languages = ["Swift", "Objective-C"]
dump(languages)
// Prints:
// ▿ 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

filter(sequence, includeElementClosure):对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包结果为true的元素合成一个新序列sequence并返回。

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))
}

find(sequence, element):返回序列sequence中某元素element的位置index。如果序列中不存在此元素,则返回nil。

var languages = ["Swift", "Objective-C"]
find(languages, "Objective-C") == 1
find(languages, "Java") == nil
find([29, 85, 42, 96, 75], 42) == 2

indices(sequence):返回序列sequence中所有元素的位置(indices是index的复数)

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

join(separator, sequence):将序列sequence通过分隔符separator连成一个字符串,并返回此字符串。

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

map(sequence, transformClosure):对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包的结果合成一个新序列sequence并返回。

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

maxElement(sequence):返回序列sequence中的最大值。

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

minElements(sequence):返回序列sequence中的最小值。

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

reduce(sequence, initial, combineClosure):给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值。

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

reverse(sequence):返回逆序的序列sequence。

equal(reverse([1, 2, 3]), [3, 2, 1])
for i in reverse([1, 2, 3]) {
// 3, 2, 1
println(i)
}

startsWith(sequence1, sequence2):如果序列sequence1中开头的元素跟序列sequence2中的所有元素都相等,则返回true,否则返回false。

startsWith("foobar", "foo") == true
startsWith(10..100, 10..15) == true
var languages = ["Swift", "Objective-C"]
startsWith(languages, ["Swift"]) == true

上面提到的函数是我认为在Swift编程中会经常用到的函数。

完整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(...)
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论