zoukankan      html  css  js  c++  java
  • 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.

     1 abs(...)
     2 advance(...)
     3 alignof(...)
     4 alignofValue(...)
     5 assert(...)
     6 bridgeFromObjectiveC(...)
     7 bridgeFromObjectiveCUnconditional(...)
     8 bridgeToObjectiveC(...)
     9 bridgeToObjectiveCUnconditional(...)
    10 c_malloc_size(...)
    11 c_memcpy(...)
    12 c_putchar(...)
    13 contains(...)
    14 count(...)
    15 countElements(...)
    16 countLeadingZeros(...)
    17 debugPrint(...)
    18 debugPrintln(...)
    19 distance(...)
    20 dropFirst(...)
    21 dropLast(...)
    22 dump(...)
    23 encodeBitsAsWords(...)
    24 enumerate(...)
    25 equal(...)                                           目前我知道equal可以判断两个数组是否相等
    26 filter(...)
    27 find(...)
    28 getBridgedObjectiveCType(...)
    29 getVaList(...)
    30 indices(...)
    31 insertionSort(...)
    32 isBridgedToObjectiveC(...)
    33 isBridgedVerbatimToObjectiveC(...)
    34 isUniquelyReferenced(...)
    35 join(...)
    36 lexicographicalCompare(...)
    37 map(...)
    38 max(...)
    39 maxElement(...)
    40 min(...)
    41 minElement(...)
    42 numericCast(...)
    43 partition(...)
    44 posix_read(...)
    45 posix_write(...)
    46 print(...)
    47 println(...)
    48 quickSort(...)
    49 reduce(...)
    50 reflect(...)
    51 reinterpretCast(...)
    52 reverse(...)
    53 roundUpToAlignment(...)
    54 sizeof(...)
    55 sizeofValue(...)
    56 sort(...)
    57 split(...)
    58 startsWith(...)
    59 strideof(...)
    60 strideofValue(...)
    61 swap(...)
    62 swift_MagicMirrorData_summaryImpl(...)
    63 swift_bufferAllocate(...)
    64 swift_keepAlive(...)
    65 toString(...)
    66 transcode(...)
    67 underestimateCount(...)
    68 unsafeReflect(...)
    69 withExtendedLifetime(...)
    70 withObjectAtPlusZero(...)
    71 withUnsafePointer(...)
    72 withUnsafePointerToObject(...)
    73 withUnsafePointers(...)
    74 withVaList(...)
     1 // assert mentioned on page 55
     2 assert(true)
     3 // countElements mentioned on page 79  原来是countElement现在是count
     4 count("foo") == 3
     5 // enumerate mentioned on page 94
     6 for (i, j) in enumerate(["A", "B"]) {
     7     // "0:A", "1:B" will be printed
     8     println("(i):(j)")
     9 }
    10 // min mentioned on page 246
    11 min(8, 2, 3) == 2
    12 // print mentioned on page 85
    13 print("Hello ")
    14 // println mentioned on page 4
    15 println("World")
    16 // sort mentioned on page 14
    17 var a = ["B","A"]
    18 sort(&a)
    19 for i in a  {
    20     // "A", "B" will be printed
    21     println(i)
    22 }

    abs(signedNumber): Returns the absolute value of a given signed number. Trivial but not documented.

    1 abs(-1) == 1
    2 abs(-42) == 42
    3 abs(42) == 42

    contains(sequence, element): Returns true if a given sequence (such as an array) contains the specified element.

    1 var languages = ["Swift", "Objective-C"]
    2 contains(languages, "Swift") == true
    3 contains(languages, "Java") == false
    4 contains([29, 85, 42, 96, 75], 42) == true

    dropFirst(sequence): Returns a new sequence (such as an array) without the first element of the sequence.

    1 languages = ["Swift", "Objective-C"]
    2 var oldLanguages = dropFirst(languages)
    3 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.

    1 languages = ["Swift", "Objective-C"]
    2 var newLanguages = dropLast(languages)
    3 equal(newLanguages, ["Swift"]) == true

    dump(object): Dumps the contents of an object to standard output.

    1 languages = ["Swift", "Objective-C"]
    2 dump(languages)
    3 // Prints:
    4 // ▿ 2 elements
    5 //   - [0]: Swift
    6 //   - [1]: Objective-C

    equal(sequence1, sequence2): Returns true if sequence1 and sequence2 contain the same elements.

    1 languages = ["Swift", "Objective-C"]
    2 equal(languages, ["Swift", "Objective-C"]) == true
    3 oldLanguages = dropFirst(languages)
    4 equal(oldLanguages, ["Objective-C"]) == true

    filter(sequence, includeElementClosure): Returns a the elements from sequence that evaluate to true by includeElementClosure.

    1 for i in filter(1...100, { $0 % 10 == 0 }) {
    2     // 10, 20, 30, ...
    3     println(i)
    4     assert(contains([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], i))
    5 }

    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.

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

    indices(sequence): Returns the indices (zero indexed) of the elements in the given sequence.

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

    join(separator, sequence): Returns the elements of the supplied sequence separated by the given separator.

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

    map(sequence, transformClosure): Returns a new sequence with the transformClosure applied to all elements in the supplied sequence.

    1 equal(map(1...3, { $0 * 5 }), [5, 10, 15])
    2 for i in map(1...10, { $0 * 10 }) {
    3     // 10, 20, 30, ...
    4     println(i)
    5     assert(contains([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], i))
    6 }

    max(comparable1, comparable2, etc.): Returns the largest of the arguments given to the function.

    1 max(0, 1) == 1
    2 max(8, 2, 3) == 8

    maxElement(sequence): Returns the largest element in a supplied sequence of comparable elements.

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

    minElements(sequence): Returns the smallest element in a supplied sequence of comparable elements.

    1 minElement(1...10) == 1
    2 languages = ["Swift", "Objective-C"]
    3 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.这个玩意一点都不懂。

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

    reverse(sequence): Returns the elements of the given sequence reversed.

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

    startsWith(sequence1, sequence2): Return true if the starting elements sequence1 are equal to the of sequence2.

    1 startsWith("foobar", "foo") == true
    2 startsWith(1...100, 1...15) == true
    3 languages = ["Swift", "Objective-C"]
    4 startsWith(languages, ["Swift"]) == true
     
     
     
     
  • 相关阅读:
    SQL中distinct的用法
    python requests 高级用法 -- 包括SSL 证书错误的解决方案
    odoo js
    线程池的理解及使用
    单点登录原理及简单实现
    如何重写hashCode()和equals()方法
    多线程中的Lock小结
    Sql语句的优化摘要
    Spring事务管理
    并发编程之原子性、可见性、有序性的简单理解
  • 原文地址:https://www.cnblogs.com/goodboy-heyang/p/4673302.html
Copyright © 2011-2022 走看看