swift3-常量与变量,int、float、double,类型推导

时间:2022-01-20 16:20:49

1.变量

//变量
var str = "Hello, playground"

var dd = 23

var PI = 3.14

var uu = true

var me = ("me",11,"33")

me.0
me.1
me.2

PI = 3.12

2.常量

//常量
let m = 89
let u = false

var a:Int
var b:String

print(a) //a 还没有初始化,不能使用

3.int、float、double

Int.min     //-9223372036854775808
Int.max     //9223372036854775807

Int64.min   //-9223372036854775808
Int64.max   //9223372036854775807

let f = 15      //15
let fa = 0xF        //15
let fb = 0o17       //15
let fc = 0b1111     //15

let fd = 1_000_000      //1000000

var oneThirdint = 1/3   //默认类型int,0
var oneThirdFloat:Float = 1/3   //小数点后有7位,0.3333333
var oneThirdDouble:Double = 1/3  //小数点后有16位,0.3333333333333333
print(oneThirdint)  //0\n
print(oneThirdFloat)    //由打印可知,精确度为小数点后6位,0.333333\n
print(oneThirdDouble)   //精确度为小数点后15位,0.333333333333333\n

4.查看推导类型type(of:XXX)

var three = 3
type(of:three)  //查看推导结果,Int.Type

var z = 0.14
type(of:z)  //Double.Type

var PI = 314e-2     //3.14
PI = 3 + 0.14159    //3.14159
PI = Double(three) + z  // 不同类型变量不能直接加减,要先类型转换,3.14