一、数组定义:
var arr = Array<String>()
或者:
var arr = [String]()
let animals = [“Giraffe”, “Cow”, “Doggie”, “Bird”]
animals.append(“Ostrich”) // 编译出错,数组不可变(let)
let animal = animals[5] // 崩溃 (超出数组索引)
// 枚举数组
for animal in animals {
println(“\(animal)”)
}
相关函数:
insert(T,atIndex:Int) // a.insert(d,atIndex:1) 在指定位置插入一个元素,结果: [a,d,b,c]
splice(Array<T> , atIndex:Int) // a.splice([d,e] , atIndex:1) 在指定位置插入一个数组
二、字典定义:
let dic = Dictionary<String,Int>()
或者:
let dic = [String:Int]()
赋值:
dic = ["Stanford":1,"Cal":10]
let ranking = dic["Ohio state"] // ranking类型为int? (会返回nil)
//用元组枚举字典
for (key,value) in dic {
println("\(key)->\(value)")
}