课程咨询 :025-66610022

  • Swift的74个常用内置函数

    发布:达内南京IOS培训      来源:达内新闻      

  • 南京达内iOS培训专家介绍一下Swift的74个常用内置函数,内置函数是指无需引入任何模块即可以直接使用的函数,非常实用,值得大家参考。

    //断言,参数如果为`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

    以上这些函数是达内IOS培训专家整理的,在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(...)

    umericCast(...)

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

上一篇:Swift与C语言指针相结合

下一篇:入门ios语言要懂得哪些知识

最新开班日期  |  更多

3G-IOS软件工程师就业班

3G-IOS软件工程师就业班

开班日期:04-28

3G-IOS软件工程师就业班

3G-IOS软件工程师就业班

开班日期:04-28

3G-IOS软件工程师周末班

3G-IOS软件工程师周末班

开班日期:04-28

3G-IOS软件工程师周末班

3G-IOS软件工程师周末班

开班日期:04-28

  • 地址:南京市玄武区洪武北路121号苏苑大厦5楼
  • 课程培训电话:025-66610022     全国服务监督电话:400-111-8989
  • 服务邮箱 tousu@tedu.cn
  • 2001-2016 达内时代科技集团有限公司 版权所有 京ICP证8000853号-56