首页 > 编程 > Swift > 正文

Swift数组详细用法解析

2020-03-09 17:47:18
字体:
来源:转载
供稿:网友

一、说明

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.isEmptyemptyArray.isEmpty// 获取元素vowels[2]// 数组越界是一个严重的错误//vowels[-1]//vowels[5]// 获取第一个元素和最后一个元素,返回的是可选型vowels.first vowels.last  //.first和.last的返回值都为可选型emptyArray.firstif let firstVowel = vowels.first{  print("The first vowel is " + firstVowel)}vowels.first!vowels[vowels.count-1]// 获取最小,最大值numbers.min() //1vowels.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 //truevar 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.countboard[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 NSArrayvar array3: NSArray = [1,"hello"]var array4: [NSObject] = [1 as NSObject,"hello" as NSObject]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到swift教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表