Swift中共有74个内建函数,但是在Swift官方文档(“The Swift Programming Language”)中只记录了7中。剩下的67个都没有记录。
本文将列举Swift所有的内建函数。本文中提到的所谓的内建函数是指那些在Swift中不需要导入任何模块(如Foundation等)或者引用任何类就可以使用的函数。
abs(signedNumber): 返回给定的有符号数字的绝对值。很简单,但是没有在文档中记录。
abs(-1) == 1
abs(-42) == 42
abs(42) == 42
contains(sequence, element): 如果给定的序列(如数组)包含特定的元素,则返回true。
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): 一个对象的内容转储到标准输出。
var languages = ["Swift", "Objective-C"]
dump(languages)
// Prints:
// ? 2 elements
// - [0]: Swift
// - [1]: Objective-C
equal(sequence1, sequence2): 如果序列1和序列2包含相同的元素,则返回true。
var languages = ["Swift", "Objective-C"]
equal(languages, ["Swift", "Objective-C"]) == true
var oldLanguages = dropFirst(languages)
equal(oldLanguages, ["Objective-C"]) == true
filter(sequence, includeElementClosure): 返回序列的一个元素,这个元素满足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))
}
find(sequence, element): 再给定的序列中返回一个指定的索引,如果在序列中没有找到这个元素就返回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): 在指定的序列中返回元素的索引(零索引)。
equal(indices([29, 85, 42]), [0, 1, 2])
for i in indices([29, 85, 42]) {
// 0, 1, 2
println(i)
}
join(separator, sequence): 返回一个由给定的分隔符分离出来的序列的元素。
join(":", ["A", "B", "C"]) == "A:B:C"
var languages = ["Swift", "Objective-C"]
join("/", languages) == "Swift/Objective-C"
map(sequence, transformClosure): 如果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
maxElement(sequence): 返回所给序列的同类元素中的最大元素。
maxElement(1...10) == 10
var languages = ["Swift", "Objective-C"]
maxElement(languages) == "Swift"
minElements(sequence): 返回所给序列的同类元素中的最小元素。
minElement(1...10) == 1
var languages = ["Swift", "Objective-C"]
minElement(languages) == "Objective-C"
reduce(sequence, initial, combineClosure): 从第一个初始值开始对其进行combineClosure操作,递归式地将序列中的元素合并为一个元素。
var languages = ["Swift", "Objective-C"]
reduce(languages, "", { $0 + $1 }) == "SwiftObjective-C"
reduce([10, 20, 5], 1, { $0 * $1 }) == 1000
reverse(sequence): 返回所给序列的倒序。
equal(reverse([1, 2, 3]), [3, 2, 1])
for i in reverse([1, 2, 3]) {
// 3, 2, 1
println(i)
}
startsWith(sequence1, sequence2):如果序列1和序列2的起始元素相等,则返回true。
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(...) |
|