//TODO:数组 <Any> 任意类型
func arrayAp(){
var bb = Dictionary<String,Float>()
var a = Array<Int>()
//增
a.append(1)
a.append(2)
//删
a.remove(at: 0)
//改
a[0] = 200
//插入
a.insert(3, at: 0)
let b = a
var c = Array<Any>()
c.append(bb)
//遍历数组
for sub in b {
print("sub = \(sub)")
}
}
字符串闭包排序
//sorted 参数必须是字符串
let names = ["Cat", "Apple", "Egg", "Block", "Dog"]
//倒序
var reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
return s1 > s2
})
let reversedNames2 = names.sorted { s1, s2 -> Bool in return s1 > s2} //简写
//顺序
var sequenceNames2 = names.sorted(by: { (s1: String, s2: String) -> Bool in
return s1 < s2
})
let a = names.sorted { (s1: String, s2: String) -> Bool in
return s1 < s2
}
let b = names.sorted { s1, s2 -> Bool in return s1 < s2} //简写
let c = names.sorted()