Swift数组详细用法解析

一、说明

Swift数组中的类型必须一致,这一点与OC不同

// 数组初始化

var numbers = [0,1,2,3,4,5]

var vowels = ["A","E","I","O","U"]

// 数组的类型: [Int] 或者 Array<Int>

//var numbers: [Int] = [0,1,2,3,4,5]

//var numbers: Array<Int> = [0,1,2,3,4,5]

// 空数组

var emptyArray1:[Int] = []

var emptyArray2:Array<Int> = []

var emptyArray3 = [Int]()

var emptyArray4 = Array<Int>()

// 创建具有默认值的数组(相同元素的数组)

var allZeros = Array<Int>(repeating: 0, count: 5)

//[0,0,0,0,0]

var allZeros2 = [Int](repeating: 0, count: 5)

//[0,0,0,0,0]

二、常用方法

var numbers = [1,2,3,4,5]

var vowels = ["A","E","I","O","U"]

var emptyArray = [Int]()

// 数组长度

vowels.count

// 判空

numbers.isEmpty

emptyArray.isEmpty

// 获取元素

vowels[2]

// 数组越界是一个严重的错误

//vowels[-1]

//vowels[5]

// 获取第一个元素和最后一个元素,返回的是可选型

vowels.first

vowels.last //.first和.last的返回值都为可选型

emptyArray.first

if let firstVowel = vowels.first{

print("The first vowel is " + firstVowel)

}

vowels.first!

vowels[vowels.count-1]

// 获取最小,最大值

numbers.min() //1

vowels.max() //U

// 使用范围

numbers[2..<4] //[3,4]

numbers[2..<numbers.count] //[3,4,5]

// 包含

vowels.contains("A")

vowels.contains("B")

let letter = "A"

if vowels.contains( letter ){

print("\(letter) is a vowel")

}

else{

print("\(letter) is not a vowel")

}

vowels.index(of: "E") //获取索引,返回值为可选型

if let index = vowels.index(of: "E"){

print("E is a vowel in position \(index+1).")

}

else{

print("E is not a vowel.")

}

// 遍历

for index in 0..<numbers.count{

numbers[index]

}

for number in numbers{

print(number)

}

for (index, vowel) in vowels.enumerated(){

//遍历数组索引和元素

print("\(index+1): \(vowel)")

}

// 比较

var oneToFive = [1,2,3,4,5]

numbers == oneToFive //true

var oneToFive2 = [1,2,4,3,5]

numbers == oneToFive //true

//swift 3.0之前数组是有序的数据集合,swift 3.0后为无序

三、更多操作

var courses = ["A","B","C"]

// 添加元素

courses.append("D") //["A","B","C","D"]

print(courses)

// 数组常量

//使用let定义的数组不可以更改任何内容

courses += ["E"] //+=后面必须和前面的类型一致 //["A","B","C","D","E"]

print(courses)

// 两个数组相加

courses = courses + ["F","G"] //+后面必须是数组

//["A","B","C","D","E","F","G"]

print(courses)

courses.insert("Q", at: 5)

//["A", "B", "C", "D", "E", "Q", "F", "G"]

print(courses)

// 删除元素

courses.removeLast()

//["A", "B", "C", "D", "E", "Q", "F"]

print(courses)

courses.removeFirst()

//["B", "C", "D", "E", "Q", "F"]

print(courses)

courses.remove(at: 4)

//["B", "C", "D", "E", "F"]

//courses.removeAtIndex(10)

print(courses)

//区间删除操作

//courses.removeRange(0..<4)

//courses.removeRange(0..<10)

//print(courses)

//courses.removeAll()

//print(courses)

// 修改元素

courses[0] = "W"

//["W", "C", "D", "E", "F"]

print(courses)

//范围修改

courses[1...3] = ["W","W","W"]

//["W", "W", "W", "W", "F"]

print(courses)

courses[0...3] = ["W"]

//["W", "F"]

print(courses)

四、二维数组

var board = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]

//var board:[[Int]] = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]

//var board:[Array<Int>] = = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]

//var board:Array<[Int]> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]

//var board:Array<Array<Int>> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]

// 二维数组获取元素

board[0]

board[0][0]

// 获取二维数组两个维度的信息

board.count

board[0].count

// Swift中的二维数组,每一维度的元素数目可以不同

board[0].append(0)

board

// 为二维数组的第一个维度添加的元素是一个数组

board.append([0,0,0,0])

board += [ [0,0,0,0] ]

board

五、NSArray

NSArray是一个类,Array是一个结构体

var array1 = [] //会默认是NSArray,swift3.0之后该写法废除

var array2 = [1,2,3,4,5] as NSArray

var array3: NSArray = [1,"hello"]

var array4: [NSObject] = [1 as NSObject,"hello" as NSObject]

以上是 Swift数组详细用法解析 的全部内容, 来源链接: utcz.com/z/325258.html

回到顶部