数组字典的常见操作
4/22/2021 Swift
数组 | 字典 | |
---|---|---|
增 | shoppingList.append("Flour") shoppingList += ["Baking Powder"] shoppingList += ["Chocolate Spread", "Cheese", "Butter"] | |
删 | let mapleSyrup = shoppingList.remove(at: 0) let apples = shoppingList.removeLast() | |
改 | var firstItem = shoppingList[0] shoppingList[0] = "Six eggs" shoppingList[4...6] = ["Bananas", "Apples"] shoppingList.insert("Maple Syrup", at: 0) | |
查 | sixDoubles.count shoppingList.isEmpty //遍历 //for-in, enumerated() for item in shoppingList { print(item) } //需要索引,用 enumerated() for (index, value) in shoppingList.enumerated() { print("Item (String(index + 1)): (value)") } |