Swift学习笔记-数组Array

前端之家收集整理的这篇文章主要介绍了Swift学习笔记-数组Array前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//泛型语法声明数组
    var myArr: Array<String>
    //使用简化语法声明数组
    var names : [String]
    var nums : [Int]

    //创建数组-使用构造器
    //创建一个空数组,并将该空数组赋值给myArr变量
    myArr = Array<String>()
    names = Array<String>(count: 10,repeatedValue: "good")
    //创建数组-使用简化数组
    var flowers:[String] = ["a","b"]
    var values = [1,2,3,"d"]
    println("\(values[3])")
    println(values[0])

    for var i = 0; i < values.count; i++
    {
        println(values[i])
    }
    //赋值
    names[1] = "lua"
    println(names)
    //for in 遍历数组
    for a in names
    {
        println(a)
    }

    //数组的增删改查
    //1.添加元素
    var languages = ["Swift"]
    //在其后追加一个元素
    languages.append("go")
    languages.append("Lua")
    println(languages)
    languages += ["Ruby"]
    languages = languages + ["niu"]
    println(languages)
    //2.插入,atIndex不能超过数组的count
    languages.insert("Good",atIndex: 0)
    //3.数组的范围
    println(languages)//[Good,Swift,go,Lua,Ruby,niu]
    println(languages[1...3])//[Swift,Lua]

    languages[1...2] = ["a","b","c"]
    println(languages)
    //删除元素
            //1.删除指定索引处的元素
    var language:Array<String> = ["OC","PHP","Swift","Ruby","Go"]
    language.removeAtIndex(2)
    println(language)//[OC,PHP,Go]
    //2.删除数组最后一位元素
    language.removeLast()
    println(language)//[OC,Ruby]
    //3.移除数组所有元素
    language.removeAll()

    //多维数组
    //二维
    var lan :[[String]]
    var lan1 : Array<Array<String>>
    //三维数组类似于二维数组
    //定义一个二维数组
    var lan3 : [[Int]]
    //初始化
    lan3 = Array<Array<Int>>(count: 4,repeatedValue: [])

猜你在找的Swift相关文章