swift 常见数据类型 包括 值类型和引用类型
值类型 包括 枚举 optional 结构体(Bool Int Float Character String Array Dictionary Set 等)
引用类型 类 class
// 声明常量用 let 修饰创建的时候就确定不能改变,声明变量用 var 修饰
let age1 = 10
let age2 : Int = 20
let age3 : Float = 30
let age4 = { () -> Int in
return 40
}()
let age5 = {
50
}()
func getAge() -> Int {
return 60
}
let age6 = getAge()
print(age1,age2,age3,age4,age5,age6)
//10 20 30.0 40 50 60
var varage1 = 10
var varage2 : Int = 20
var varage3 : Float = 30
var varage4 = { () -> Int in
return 40
}()
var varage5 = {
50
}()
var varage6 = getAge()
print(varage1,varage2,varage3,varage4,varage5,varage6)
//10 20 30.0 40 50 60
元组 是swift 相对OC的 可以灵活的声明多种数据组合。
let err = (404,"Not Found")
let err2 = (500,"No Data")
var err3 : (Int,String,Int?)? = nil
err3 = (100,"err",0)
err3 = (101,"err",nil)
var err4 : (errCode : Int, errMsg : String,otherCode : Int?)? = nil
err4 = (102,"fdsfs",123)
print(err)
print(err2)
print(err3!)
print(err4!)
let (code,msg,other) = err4!
print("\(code),\(msg),\(other ?? 0)")
//(404, "Not Found")
//(500, "No Data")
//(101, "err", nil)
//(errCode: 102, errMsg: "fdsfs", otherCode: Optional(123))
//102,fdsfs,123
网友评论