V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
maiziedu
V2EX  ›  程序员

swift 2.2 数组的使用

  •  
  •   maiziedu · 2016-12-21 18:03:18 +08:00 · 1580 次点击
    这是一个创建于 2681 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本文和大家分享的主要是 swift2.2 中的数组相关用法,希望通过本文的分享,对大家有所帮助,一起来看看吧。

    什么是数组

    1.抽象数据类型的一种;

    2.是一串有序的由相同类型元素构成的集合;

    3.swift 数组使用有序存储同一类型的多个值。相同的值可以多次出现在一个数组中的不同位置中;

    4.swift 数组会强制检测元素的类型,如果类型不同会报错,遵循像 Array < Element >;这样的形式,其中 Element 是这个数组中唯一允许存在的数据类型;

    5.数组常量不可修改元素值和大小;数组变量可任意进行增删改。

    swift 数组相关操作

    1.创建数组;

    2.访问数组;

    3.修改数组;

    4.遍历数组;

    5.合并数组;

    6.COUNT , isEmpty 属性。

    swift 集合

    1.swift 提供 Array,Set,Dictonary 三种集合类型来存储集合数据;

    2.swift 数组 Array 是有序数据的集。

    实例

    //: Playground - noun: a place where people can play

    import UIKit

    var emptyArray = Int

    emptyArray.dynamicType

    var someInts = [Int](count:3, repeatedValue:0)

    var anotherInts = [10, 20, 30]

    var copyInts = anotherInts

    var addInts = anotherInts + copyInts

    anotherInts[0]

    anotherInts[1]

    anotherInts[2]

    // 0 1 2

    anotherInts.append(40)

    anotherInts

    // +=

    anotherInts += [50, 60]

    anotherInts.insert(-10, atIndex: 0)

    anotherInts[0] = 0

    anotherInts

    let range = 0...3

    anotherInts[range] = [-1,-2,-3,-4]

    anotherInts

    let constantArr = Int

    for item in anotherInts {

    print(item)

    }

    for (index, item) in anotherInts.enumerate() {

    print(index, item)

    }

    anotherInts.count

    emptyArray.count

    anotherInts.isEmpty

    emptyArray.isEmpty

    原文链接: http://www.maiziedu.com/wiki/swift/array/

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3238 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:06 · PVG 20:06 · LAX 05:06 · JFK 08:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.