💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## Collection Types ~~~ // arr随着brr改变 var arr = ["hello", "world"] var brr = arr brr[0] = "haw" brr // ["haw", "world"] arr // ["haw", "world"] // arr不随brr改变,说明brr原本与arr指向一块内存,以下操作后指向新的内存,并把数组中的元素值copy了一遍。 // 长度发生变化时,Array会发生拷贝 var arr = ["hello", "world"] var brr = arr brr[0..0] = ["haw"] brr // ["haw", "hello", "world”] arr // ["hello", "world"] // arr不随brr改变,同上 var arr = ["hello", "world"] var brr = arr brr.insert("haw", atIndex: 0) // remove也一样 brr // ["haw", "hello", "world”] arr // ["hello", "world"] ~~~ ~~~ for (index, value) in enumerate(shoppingList) { println("Item \(index + 1): \(value)") } // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas ~~~ ~~~ var threeDoubles = Double[](count: 3, repeatedValue: 0.0) // threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0] var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) // anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5] ~~~ The only restriction is that KeyType must be hashable—that is, it must provide a way to make itself uniquely representable. All of Swift’s basic types (such as String, Int, Double, and Bool) are hashable by default, and all of these types can be used as the keys of a dictionary. Enumeration member values without associated values (as described in Enumerations) are also hashable by default. ~~~ // 以下将字典airports中key为DUB的值更新为Dublin International,返回的是它原来的值 if let oldValue = airports.updateValue("Dublin International", forKey: "DUB") { println("The old value for DUB was \(oldValue).") } // prints "The old value for DUB was Dublin. ~~~ You can also use subscript syntax to retrieve a value from the dictionary for a particular key. Because it is possible to request a key for which no value exists, a dictionary’s subscript returns an optional value of the dictionary’s value type. ~~~ airports["APL"] = "Apple International" // "Apple International" is not the real airport for APL, so delete it airports["APL"] = nil // APL has now been removed from the dictionary if let removedValue = airports.removeValueForKey("DUB") { println("The removed airport's name is \(removedValue).") } else { println("The airports dictionary does not contain a value for DUB.") } // prints "The removed airport's name is Dublin International. // for airportCode in airports.keys { println("Airport code: \(airportCode)") } // Airport code: TYO // Airport code: LHR for airportName in airports.values { println("Airport name: \(airportName)") } // Airport name: Tokyo // Airport name: London Heathrow let airportCodes = Array(airports.keys) // airportCodes is ["TYO", "LHR"] let airportNames = Array(airports.values) // airportNames is ["Tokyo", "London Heathrow"] ~~~ Arrays and dictionaries store multiple values together in a single collection. If you create an array or a dictionary and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the size of the collection after it is created by adding more items to the collection, or by removing existing items from the ones it already contains. Conversely, if you assign an array or a dictionary to a constant, that array or dictionary is immutable, and its size cannot be changed. For dictionaries, immutability also means that you cannot replace the value for an existing key in the dictionary. An immutable dictionary’s contents cannot be changed once they are set. Immutability has a slightly different meaning for arrays, however. You are still not allowed to perform any action that has the potential to change the size of an immutable array, but you are allowed to set a new value for an existing index in the array. This enables Swift’s Array type to provide optimal performance for array operations when the size of an array is fixed.