2、for character in "Dog!" { println(character) } // D // o // g // !
let unusualMenagerie = "Koala , Snail , Penguin , Dromedary " println("unusualMenagerie has \(countElements(unusualMenagerie)) characters") // 打印输出:"unusualMenagerie has 36 characters
是否有前缀:scene.hasSuffix("Capulet's mansion") 是否有后缀:scene.hasSuffix("Friar Lawrence's cell")
根据索引新增元素:shoppingList.insert("Maple Syrup", atIndex: 1) 对应删除:let mapleSyrup = shoppingList.removeAtIndex(1) 移除最后一项:let apples = shoppingList.removeLast()
for (index, value) in enumerate(shoppingList) { println("Item \(index + 1): \(value)") }
var threeDoubles = Double[](count: 3, repeatedValue:0.0) // threeDoubles 是一种 Double[]数组, 等于 [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]
if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB") { println("The old value for DUB was \(oldValue).====dic\(airports)") }
airports["APL"] = nil // APL现在被移除了
let airportCodes = Array(airports.keys)
13、不可变字典的内容在被首次设定之后不能更改。不可变行对数组来说有一点不同,当然我们不能试着改变任何不可变数组的大小,但是我们·可以重新设定相对现存索引所对应的值。在我们不需要改变数组大小的时候创建不可变数组是很好的习惯。如此 Swift 编译器可以优化我们创建的集合。
2014年07月02日