An array of
- Creating an empty array
var someInts = [Int]() // []
someInts.append(3) // [3]
someInts = [] // []
Copy the code
- Creates an array with default values
Var threeDoubles = Array(repeating: 0.0, count: 3) // [0, 0, 0] var anotherThreeDoubles = Array(repeating: 2.5, count: 3) 5) // [2.5, 2.5] var sixDoubles = Doubles + anotherThreeDoubles // 2.5] var shoppingList: [String] = [" dense Eggs ", "Milks"] / / / "dense Eggs", "Milks" var shopList = [" dense Eggs ", "Milks"] / [" dense Eggs." "Milks"]Copy the code
- Check whether the array is empty
If shoppingList. IsEmpty {print(" check whether the array isEmpty ")}Copy the code
shoppingList.count
shoppingList.append("Flour") // ["Eggs", "Milks", "Flour"]
shoppingList += ["Cheese"] // ["Eggs", "Milks", "Flour", "Cheese"]
var firstItem = shoppingList[0]
shoppingList[0] = "Six eggs"
Copy the code
- Gets the deleted array element
let mapleSyrup = shoppingList.remove(at: 0)
let cheese = shoppingList.removeLast()
Copy the code
- Through the array
for item in shoppingList {
print(item)
}
for (index,value) in shoppingList.enumerated() {
print("item \(index) - \(value)")
}
Copy the code
A collection of
- Creating an empty collection
var letters = Set<Character>()
Copy the code
letters.count
letters.insert("a")
letters = []
Copy the code
var favoriteGenres:Set<String> = ["Rock","Classical","Hip hop"] // {"Rock", "Hip hop", "Classical"}
var favoriteSet:Set = ["Rock","Classical","Hip hop"]
Copy the code
- Determines whether the set is empty
Yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes, yes.Copy the code
if let removedGenre = favoriteGenres.remove("Rock") { print(removedGenre) } if favoriteGenres.contains("Classical") { Genres {print(genre)} for genre in favoriteGenres {print(genre)} for genre in favoriteGenres. Sorted () {print(genre) }Copy the code
The dictionary
- Creating an empty dictionary
var nameOfIntegers = [Int:String]()
nameOfIntegers[16] = "sixteen"
nameOfIntegers = [:]
Copy the code
var airport:[String:String] = ["YYZ":"Toronto","DUB":"Dublin"]
var air = ["YYZ":"Toronto","DUB":"Dublin"]
airport.count
airport["LHR"] = "London"
airport.updateValue("Dublin Airport", forKey: "DUB")
airport.removeValue(forKey: "DUB") // ["LHR": "London", "YYZ": "Toronto"]
Copy the code
- Check if the dictionary is empty
If airport. IsEmpty {print(" check whether the dictionary isEmpty ")}Copy the code
- The dictionary traversal
for (airportCode,airportName) in airport {
print("code : \(airportCode) - name : \(airportName)")
}
/*
code : LHR - name : London
code : YYZ - name : Toronto
*/
for airportCode in airport.keys {
print(airportCode)
}
/*
LHR
YYZ
*/
for airportName in airport.values {
print(airportName)
}
/*
London
Toronto
*/
Copy the code
- Dictionary keys and values build array collections
let airportCodes = [String](airport.keys) // ["LHR", "YYZ"]
let airportNames = [String](airport.values) // ["London", "Toronto"]
Copy the code