?
Araay是有序的數據集,在OC中分為不可變數組NSArray和可變數組NSMutableArray,在swift中只有常量和變量兩種類型,聲明成變量那就可以說明是可變的了!
?
學習時的具體的用法總結成如下的代碼:
?
//數組var arrInts = [Int]()//創建一個空數組arrInts = [];print("arrInts is of type [Int] with \(arrInts.count) items.")//// 打印 "someInts is of type [Int] with 0 items."
var threeDoubles = Array(repeating:0.0,count:3)//創建一個帶有默認值的數組print("threeDoublesArray:\(threeDoubles)")//打印 threeDoublesArray:[0.0, 0.0, 0.0]threeDoubles += threeDoubles//數組合并print("threeDoubles:\(threeDoubles)")//打印 threeDoubles:[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
let anotherThreeDoubles = Array(repeating: 2.5, count: 3)let sixDoubles = threeDoubles + anotherThreeDoubles;//數組合并print(sixDoubles)//打印 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.5, 2.5, 2.5]//用數組字面量構造數組var goodsListArr:[String] = ["onions","eggs","apple","orange","pear","salt"]goodsListArr.append("vinegar")//在數組的末尾添加一個元素(不可以用下標訪問的形式去在數組尾部添加新項)goodsListArr += ["Chocolate Spread", "Cheese", "Butter"]//給數組添加幾個元素if goodsListArr.isEmpty {//判斷數組是否為空print("The shopping list is empty.")} else {print("The shopping list is not empty.")}// 打印 "The shopping list is not empty."(shoppinglist 不是空的)
let firstItem = goodsListArr[0]//根據索引 取對應的索引值print("firstItemValue:\(firstItem)")//打印 firstItemValue:onions
goodsListArr[0] = "eight onions"//將第一個索引值替換掉// 其中的第一項現在是 "Six onions" 而不是 "onions"
print("Replace the former results:\(goodsListArr)")//替換前的結果 Replace the former results:["eight onions", "eggs", "apple", "orange", "pear", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"]goodsListArr[2...4] = ["Bananas", "Apples"]//將某個范圍的值替換掉print("results of substitution:\(goodsListArr)")//替換后的結果 results of substitution:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"]//在數組中插入元素(調用數組的insert(_:at:)方法來在某個具體索引值之前添加數據項)goodsListArr.insert("books", at: 0)//在0索引之前添加數據,現在數組第一個元素是“books”//根據索引移除數組中某一個元素
let removeItem = goodsListArr.remove(at: 0)//將數組的第一個元素移除并獲取被移除的第一項元素print("removed index 0 item is:\(removeItem) After removing the results:\(goodsListArr)")//removed index 0 item is:books After removing the results:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"]//如果我們試著對索引越界的數據進行檢索或者設置新值的操作,會引發一個運行期錯誤。我們可以使用索引值和數組的count屬性進行比較來在使用某個索引之前先檢驗是否有效。除了當count等于 0 時(說明這是個空數組),最大索引值一直是count - 1,因為數組都是零起索引
let lastItem = goodsListArr.removeLast()//將數組的最后一個元素移除并獲取被移除的最后一個元素值
print("removed last item is:\(lastItem) After removing the results:\(goodsListArr)")//打印 removed last item is:Butter After removing the results:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese"]for item in goodsListArr{//數組的遍歷
print("數組遍歷的結果:\(item)")/* 打印數組遍歷的結果:eight onions數組遍歷的結果:eggs數組遍歷的結果:Bananas數組遍歷的結果:Apples數組遍歷的結果:salt數組遍歷的結果:vinegar數組遍歷的結果:Chocolate Spread數組遍歷的結果:Cheese*/}//使用enumerated()方法來進行數組遍歷。enumerated()返回一個由每一個數據項索引值和數據值組成的元組。我們可以把這個元組分解成臨時常量或者變量來進行遍歷(可以同時d得到每個數據項的值和索引值)for(index,value) in goodsListArr.enumerated(){print("Item \(String(index + 1)): \(value)")/*打印Item 1: eight onionsItem 2: eggsItem 3: BananasItem 4: ApplesItem 5: saltItem 6: vinegarItem 7: Chocolate SpreadItem 8: Cheese*/}
這是我近期在學習swift的學習總結,給朋友們提供學習參考,同時發現有錯誤的地方可以指出相互交流學習共同進步!
?