• Welcome to Swift (苹果官方Swift文档初译与注解二十二)---148~153页(第三章--集合类型)


      在数组的指定索引位置插入一个元素,可以调用数组的insert(atIndex:)方法:

        shoppingList.insert("Maple Syrup", atIndex: 0)

        // shoppingList now contains 7 items

        // "Maple Syrup" is now the first item in the list”

      例子中的insert方法在数组的开始位置(索引为0)插入一个新的元素,元素的值是"Maple Syrup"

      同样的,你也可以使用removeAtIndex方法从数组中移除一个元素.这个方法会移除数组指定索引位置的元素,并且返回这个被移除的元素(尽管你可能不需要这个返回值):

        let mapleSyrup = shoppingList.removeAtIndex(0)

        // the item that was at index 0 has just been removed

        // shoppingList now contains 6 items, and no Maple Syrup

        // the mapleSyrup constant is now equal to the removed "Maple Syrup" string”

      当数组中的一个元素被移除后,后面的其他元素会占据移除之后的空白.因此,例子代码中数组中索引0的位置的值再一次的等于"Six eggs":

        irstItem = shoppingList[0]

        // firstItem is now equal to "Six eggs”

      如果你想移除数组最后面的元素,可以使用removeLast方法,而不必使用 removeAtIndex 方法,这样可以避免查询数组的conut属性.与 removeAtIndex 方法类似,removeLast方法同  样返回被删除的元素:

        let apples = shoppingList.removeLast()

        // the last item in the array has just been removed

        // shoppingList now contains 5 items, and no cheese

        // the apples constant is now equal to the removed "Apples" string”

    Iterating Over an Array (遍历数组)

      可以使用for-in循环来变量整个数组:

        for item in shoppingList {

            println(item)

        }

        // Six eggs

        // Milk

        // Flour

        // Baking Powder

        // Bananas

      如果你需要知道每个索引和它对应的元素的值,可以使用全局函数 enumerate来遍历数组.全局函数 enumerate返回一个元组,在元组中包含着数组索引和索引对应的元素值.可以从  元组中把它们解析出来保存到临时常量或变量,再把常量或变量作为遍历的一部分使用:

        for (index, value) in enumerate(shoppingList) {

            println("Item (index + 1): (value)")

        }

        // Item 1: Six eggs

        // Item 2: Milk

        // Item 3: Flour

        // Item 4: Baking Powder

        // Item 5: Bananas

    Creating and Initializing an Array (创建数组和初始化数组)

      使用初始化语法可以创建一个某种类型的空的数组(不用设置任何初始值):

        var someInts = Int[]()

        println("someInts is of type Int[] with (someInts.count) items.")

        // prints "someInts is of type Int[] with 0 items.”

      注意变量someInts 的类型是 Int[],因为它使用了Int[]来初始化.

      如果内容的类型信息已经指定(比如一个函数的参数,或者已经定义类型的变量或者常量),可以使用空白的中括号[]来创建一个空数组:

        someInts.append(3)

        // someInts now contains 1 value of type Int

        someInts = []

        // someInts is now an empty array, but is still of type Int[]

      在Swift中,数组类型也提供了一个使用提供的默认值来创建固定大小数组的初始化方法.你需给初始化方法传递元素的个数(count)和它对应类型的默认值(repeatedValue):

        var threeDoubles = Double[](count: 3, repeatedValue: 0.0)

        // threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]

      由于Swift的类型判断机制,当使用初始化方法来创建数组的时候,可以不必指定数组要存储的类型,因为Swift可以通过默认值来确定数组存储值的类型:

        var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5)

        // anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]

      最后,你可以使用加法操作符来创建一个新的数组,新的数组的类型可以根据加法操作符的两个已经存在的数组判断出来:

        var sixDoubles = threeDoubles + anotherThreeDoubles

        // sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

  • 相关阅读:
    微擎模块机制分析2
    微擎we7模块和模板安装方法
    git的使用 及一些常见的错误处理
    人,活着为了什么?
    fedora配置ip
    fedora安装gcc
    linux查看内核版本和发行版本号
    python数据类型2
    python之零碎知识
    python之数据类型1
  • 原文地址:https://www.cnblogs.com/caios/p/3815456.html
Copyright © 2020-2023  润新知