元组
var t = (First : 1, Second : 2, Third : 3)//三元组,可以进行命名调用也可以使用位置调用
t.0 = 4
t.First = 6
print(t.0, t.1, t.2)
print(t)
func returnt(a : Int, b : Int) -> (Int, Int) {//可返回多个值
return(a * 3, b + 4)
}
var t1 = returnt(4, b: 3)
print(t1.0, t1.1)
defer
func d() {//延迟执行,在函数执行一遍后执行
var i = 0
defer {
print(i)
}
i = 2
defer {//后添加的延迟先执行
print(i)
}
i = 3
print(i)
}
autoclosure
print(d())
func xx(@autoclosure q:()->Bool) {//自动闭包
if q(){
print("True")
}
else{
print("False")
}
}
xx(1>2)
any/anyobject
- any指定为任意类型
- anyobject只能为class
final
- 在父类方法前加上final,子类便不能重构父类的方法
inout
var (x, y) = (11.0, 22.0)
func fool(inout x: Double, inout y : Double) {
(x, y) = (y, x)
}
fool(&x , y: &y)
print(x, y)
lazy
class sky {
lazy var name : String = {
var blue = "blue"
print("正在初始化。。。")
return blue
}()
var red = "red"
func pink() {
}
}
var sky1 = sky()
print(sky1.name, sky1.red)
print(sky1.name)
泛型函数与泛型类(模版函数/模版类)
func sky2<T>(t: T) {
print("this is :", t)
}
var ss1 = 123
var ss2 = "123"
var a = [1, 12, 23]
sky2(ss1)
sky2(ss2)
sky2(a)
class sky3<T> {
var color : T!
}
let s1 = sky3<Int> ()
s1.color = 123
let s2 = sky3<String>()
s2.color = "blue"
print(s1.color, s2.color)