swift 代码改变世界

The Swift Programming Language(4

2015-04-10  本文已影响193人  宋翰要长肉

Collection Types

Array

An array stores multiple values with same type in an ordered list

Array Type Shorthand Syntax

Swift数组类型的完整表示为ArraySomeType是数组可以存储元素的类型名称

数组类型的简略表示为[SmoeType]

Array Literals

一个数组可以由array literal初始化。一个array literal为一个由square brackets包裹,由commas分割的数值的序列:


[value 1, value 2, value 3] 

Accessing and Modifying an Array

Property

find number of items in an array

check count property is equal to 0

Method

add a new item to the end of an array

insert an item in an array

remove an item from an array and return the item

remove last item from an array and return the item

subscript syntax


1. var numberArray = [1,2,3,4,5,6]  

2. numberArray[0] = 0

3. //numberArray now [0,2,3,4,5,6,]


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


1. for item in shoppingList {

2. println(item)

3. }


1. for (index, value) in enumerate(shoppingList) {

2. println("Item \(index+1):\(value)")

3. }

Creating and Initializing an Array


1. var someIns = [Int]()

2. println("someInts is of type [Int] with \(someInt.count) items.")


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类型的完整表示为Dictionarykey 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

Methods

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

1. airports​[​"LHR"​] = ​"London"


1. airports["LHR"] = "London Heathrow"


1. airports["APL"] = "Apple International"

2. println("5. \(airports)")

3. airports["APL"] = nil

4. println("5. \(airports)")

Iterating Over a Dictionary


1. for (airportCode, airportName)  in airports {

2.    println("6. \(airportCode): \(airportName)")

3. }


1. for airportCode in airports.keys{

2.    println("7. Airport code: \(airportCode)")

3. }


1. for airportName in airports.values {

2.    println("8. Airport name: \(airportName)")

3. }


1. let airportCodes = [String](airports.keys)

2. println(airportCodes)


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:

上一篇 下一篇

猜你喜欢

热点阅读