Swift数组详细用法解析

时间:2022-05-14 04:26:02

一、说明

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 数组初始化
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]

二、常用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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后为无序

三、更多操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)

四、二维数组

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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是一个结构体

?
1
2
3
4
5
6
7
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]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。