Collection Types
Dictionary
Array
Array
An array stores multiple values with same type in an ordered list
Array Type Shorthand Syntax
Swift数组类型的完整表示为Array
,SomeType
是数组可以存储元素的类型名称
数组类型的简略表示为[SmoeType]
Array Literals
一个数组可以由array literal初始化。一个array literal为一个由square brackets包裹,由commas分割的数值的序列:
[value 1, value 2, value 3]
Accessing and Modifying an Array
Property
count
find number of items in an array
isEmpty
check count
property is equal to 0
Method
-
append
or+=
add a new item to the end of an array
insert(newElementValue, atIndex:Int)
insert an item in an array
removeAtIndex()
remove an item from an array and return the item
removeLast()
remove last item from an array and return the item
subscript syntax
- change an existing value
1. var numberArray = [1,2,3,4,5,6]
2. numberArray[0] = 0
3. //numberArray now [0,2,3,4,5,6,]
- change a range of values
1. var numberArray = [1,2,3,4,5,6]
2. numberArray[0...3] = [0,0]
3. //numberArray now [0,0,5,6]
Iterating Over an Array
- iterate over entire values of array with
for
-in
loop
1. for item in shoppingList {
2. println(item)
3. }
- iterate over entire values and index of array
1. for (index, value) in enumerate(shoppingList) {
2. println("Item \(index+1):\(value)")
3. }
Creating and Initializing an Array
- create an empty array
1. var someIns = [Int]()
2. println("someInts is of type [Int] with \(someInt.count) items.")
- create an array of certain size(called
count
) and default value(calledrepeatedValue
)
1. var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
2. println("13. threeDoubles: \(threeDoubles)")
3. var anotehrThrreeDoubles = [Double](count: 3, repeatedValue: 2.5)
4. var sixDoubles = threeDoubles + anotehrThrreeDoubles5. println("13. sixDoubles: \(sixDoubles)")
Dictionary
Each value in dictionary is associated with a unique key. Items in dictionary don't have an order
Dictionary Type Shorthand syntax
Swift Dictionary类型的完整表示为Dictionary
,key
is type of key, value
is type of value
数组类型的简略表示为[key, value]
Dictionary Literal
1. var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Accessing and Modifying a Dictionary
Property
count
isEmpty
Methods
updateValue(forKey:)
The type of return result is optional(String?
), and the return result is old value.
If there is no value for the key, the dictionary will add this element and return nil
1. if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
2. println("4. The old value for DUB was \(oldValue).")
3. }
Subscript Syntax
- add an element with new key
1. airports["LHR"] = "London"
- modify an element with a key
1. airports["LHR"] = "London Heathrow"
- remove an element with a key
1. airports["APL"] = "Apple International"
2. println("5. \(airports)")
3. airports["APL"] = nil
4. println("5. \(airports)")
Iterating Over a Dictionary
- iterate key and value
1. for (airportCode, airportName) in airports {
2. println("6. \(airportCode): \(airportName)")
3. }
- iterate key
1. for airportCode in airports.keys{
2. println("7. Airport code: \(airportCode)")
3. }
- iterate value
1. for airportName in airports.values {
2. println("8. Airport name: \(airportName)")
3. }
- use a dictionary's key to initialize an array
1. let airportCodes = [String](airports.keys)
2. println(airportCodes)
- use a dictionary's value to initialize an array
1. let airportNames = [String](airports.values)
2. println(airportNames)
Creating an Empty Dictionary
1. var namesOfIntegers = [Int: String]()
Hash Values for Dictionary Key Types
The type of key must provide a way to compute a hash value for itself
Hashable type:
Int
String
Double
Bool
Enumeration (member values without associated values)