1.常量
在swift中,我们定义常量是用let,它只能赋值一次,而且需要在使用之前必须赋值一次。
例子:
let a = 10
//或者
let a2 :Int
a2 = 102.变量
在swift中,我们定义变量是用var,通过赋值,编译器会自动推断出变量的类型。
例子:
let a = 10let b = 20
var c = a + bvar str = "变量"3.数据类型
- 值类型:Bool,Int,Float,Double,Character,String,Array,Dictionary,Set
- 引用类型:class
例子:
//布尔 let bool = true
//字符串 let string = "字符串"
//字符 let character:Character = "C"
//整数 let int = 10
//浮点数 let double = 10.0
//数组 let array = [1,3,5,7,9]
//字典 let dictionary = ["name":"张三","age":10,"sex":"男"]4.类型转换
这里描述的类型转换先展示浅显的,更深次性待后面补充。
例子:
let int = 3
let double = 0.88
let result = Double(int) + double当然,如果字面量是数字是可以直接添加的。
例子:
let result = 3 + 0.88元组(Tuple)
元组是一种复合类型,元组中的每一种类型都可以是任何的结构体、枚举或类类型。
例子:
let http404Error = (404,"notFound")
print("the status code is \(http404Error.0)") 当然我们也可以给元组指定标签,通过访问标签来访问元素。
例子:
let http404Error = (statuscode:404,descript:"notFound")
print("the status code is \(http404Error.statuscode)")或者直接就用定义元组去赋值。
例子:
let (statuscode,descript) = (404,"notFound")
print("the status code is \(statuscode)")下一遍文章:swift从入门到精通02-流程控制