Array
创建
var array1 = []
var array2 : [Int] = [1, 2, 3, 4]
var array3 : [Int] = [Int]()
var array4 = [1, "message", true, [1, 2, 3, 4]]
print(type(of: array4))
遍历
var items = ["A", "B", "C", "D"]
for item in items {
print(item)
}
for (index, item) in items.enumerated() {
print("index: \(index), item: \(item)")
}
插入
var items = ["A", "B", "D", "E"]
items.insert("C", at: 2)
print(items)
删除
var items = ["A", "B", "C", "D"]
print(items)
items.remove(at: 2)
print(items)
Dictionary
创建
var dictionary1 = [:]
var dictionary2[Int: String] = [Int: String]()
var dictionary3 = ["name": "Bruce", "age": 10, "working": yes]
print(type(of: dictionary3))
遍历
var info = ["name": "Bruce", "age": 10, "working": yes]
or (key, value) in info {
print("key: \(key), value: \(value)")
}