Swift5 数组(Array)操作
2020-03-24 本文已影响0人
圣艾修
Swift数组创建
// 指定内容类型
let arr : [Int] = [1, 3, 5, 7]
print(arr)
// 结果:[1, 3, 5, 7]
let lookup: [String: Int] = ["A":1]
print(lookup)
// 结果:["A": 1]
let list : [Any] = ["a", 2, "chah", 2.5]
print(list)
// 结果:["a", 2, "chah", 2.5]
// 重复数据快速创建
let array = [String](repeating: "A", count: 3)
print(array)
// 结果:["A", "A", "A"]
// 空数组
let array1 = [Float]()
print(array1)
// 结果:[]
var array2: Array<Int> = []
print(array2)
// 结果:[]
Swift获取指定位置数据
let arr : [Int] = [1, 3, 5, 7]
// 第一个数据
print(arr.first!)
// 结果:1
// 指定位置,注意数组越界
print(arr[0])
// 结果:1
// 最后一个数据
print(arr.last!)
// 结果:7
Swift数组遍历 for、map、flatMap、reduce
// for 遍历
let arr : [Int] = [1, 3, 5, 7]
for (value, index) in arr.enumerated() {
print("value= \(value) index= \(index)")
}
/**
value= 0 index= 1
value= 1 index= 3
value= 2 index= 5
value= 3 index= 7
*/
// 或者不需要index
for value in arr {
print("value= \(value) ")
}
/**
value= 1
value= 3
value= 5
value= 7
*/
// map 遍历
let cast = ["Vivien", "Marlon", "Kim", "Karl", ""]
let lowercaseNames = cast.map { $0.lowercased() }
print(lowercaseNames)
// 结果:["vivien", "marlon", "kim", "karl", ""]
let flat = cast.map{ Array(repeating: $0, count: $0.count) }
print(flat)
// 结果:[["Vivien", "Vivien", "Vivien", "Vivien", "Vivien", "Vivien"], ["Marlon", "Marlon", "Marlon", "Marlon", "Marlon", "Marlon"], ["Kim", "Kim", "Kim"], ["Karl", "Karl", "Karl", "Karl"], []]
// flatMap 遍历
let flatMapped = cast.flatMap{ Array(repeating: $0, count: $0.count) }
print(flatMapped)
// 结果:["Vivien", "Vivien", "Vivien", "Vivien", "Vivien", "Vivien", "Marlon", "Marlon", "Marlon", "Marlon", "Marlon", "Marlon", "Kim", "Kim", "Kim", "Karl", "Karl", "Karl", "Karl"]
// reduce 遍历
let arr : [Int] = [1, 2, 3, 4]
let numberSum = arr.reduce(0, { x, y in
x + y
})
print(numberSum)
// 结果:10
// filter 遍历
var students = ["A", "Cs", "Boy", "G", "E"]
students.sort(by: >)
let stu = students.filter({ string -> Bool in
return string.count > 1
})
print(stu)
// 结果: ["Cs", "Boy"]
区别
map可以对数组元素做处理,返回新的数组
flatMap可以打开多维数组,变成一个数组,返回新的数组
flatMap返回没有空值
filter 根据条件筛选数组,返回新的数组
reduce可以对数组的元素进行计算
Swift数组添加
var arr : [Int] = [1, 3, 5, 7]
// 添加单个数据
arr.append(9)
print(arr)
// 结果: [1, 3, 5, 7, 9]
let arry = [0, 10]
// 添加多个数据
arr.append(contentsOf: arry)
print(arr)
// 结果: [1, 3, 5, 7, 9, 0, 10]
// 从范围内添加数据
arr.append(contentsOf: stride(from: 11, through: 20, by: 2))
print(arr)
// 结果:[1, 3, 5, 7, 9, 0, 10, 11, 13, 15, 17, 19]
Swift数组插入
var array: Array<Int> = []
// 插入多个数据
array.insert(contentsOf: [1,4,5], at: 0)
print(array)
// 结果: [1, 4, 5]
// 插入单个数据
array.insert(6, at: 2)
print(array)
// 结果:[1, 4, 6, 5]
Swift数组最值
print(array.max())
print(array.min())
数组包含
print(array.contains(6))
Swift数组内容数量
var arr : [Int] = [1, 3, 5, 7]
print(arr.count)
print(arr.capacity)
Swift数组capacity代表容量,自动扩充
每个数组都保留特定的内存量来保存其内容。当向数组中添加元素时,该数组开始超出其保留容量,该数组将分配更大的内存区域并将其元素复制到新存储中。
Swift数组删除
var arr : [Int] = [1, 3, 5, 7, 9, 11, 13]
// 删除第一个
arr.removeFirst()
print(arr)
// 结果:[3, 5, 7, 9, 11, 13]
// 删除前两个
arr.removeFirst(2)
print(arr)
// 结果:[5, 7, 9, 11, 13]
// 删除最后一个
arr.removeLast()
print(arr)
// 结果:[7, 9, 11]
// 删除最后二个
arr.removeLast(2)
print(arr)
// 结果:[7]
// 清空
arr.removeAll()
print(arr)
// 结果:[]
Swift数组合并
let intsA = [Int](repeating: 2, count:2)
let intsB = [Int](repeating: 1, count:3)
let intsC = intsA + intsB
print(intsC)
// 结果:[2, 2, 1, 1, 1]
用append也可以
Swift数组排序
// 修改原数组
var arr : [Int] = [1, 3, 5, 9, 2, 4]
arr.sort()
print(arr)
// 结果: [1, 2, 3, 4, 5, 9]
// 另起数组
let array : [Int] = [1, 3, 5, 9, 2, 4]
print(array.sorted())
// 结果: [1, 2, 3, 4, 5, 9]
// 降序排列
var students = ["A", "C", "B", "G", "E"]
students.sort(by: >)
print(students)
// 结果: ["G", "E", "C", "B", "A"]
// 按对象排序
let someDict:[String:String] = ["key":"2"]
let someDict1:[String:String] = ["key":"5"]
let someDict2:[String:String] = ["key":"3"]
let arr = [someDict, someDict1, someDict2]
let numberSum = arr.sorted(by: {(obj1, obj2) -> Bool in
return obj1["key"]! > obj2["key"]!
})
print(numberSum)
// 结果: [["key": "5"], ["key": "3"], ["key": "2"]]
sorted返回新数组 sort无返回,操作原数组
Swift5 字符串(String)操作
Swift5 字符(Character)操作
Swift5 数组(Array)操作
Swift5 字典(Dictionary)操作
Swift5 集合(Set)操作
Swift5 简单使用
Swift OC动态调试工具 InjectionIII 使用