Swift數組詳細用法解析。本站提示廣大學習愛好者:(Swift數組詳細用法解析)文章只能為提供參考,不一定能成為您想要的結果。以下是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]
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。