let num1 = 10
let num2: Int
num2 = 20
func getNum() -> Int{
return 30
}
let num3 = getNum()
func (){
print("v587")
}
()
let = "牛"
var = "猪头"
Swift 中是没有基本数据类型概念的,只有值类型与引用类型。
常见数据类型 | ||
---|---|---|
值类型(value type) | 枚举(enum) | Optional |
结构体(struct) | Bool、Int、Float、Double、Character | |
String、Array、Dictionary、Set | ||
引用类型(reference type) | 类(class) |
//布尔
let bool1 = true //取反是false
let bool2 = false
//字符串
let string = "CSDN"
//字符(可存储ASCII字符、Unicode字符)
let character :Character = ""
//整数
let intDecimal = 16 // 十进制
let intBinary = 0b10000 // 二进制
let intOctal = 0o20 // 八进制
let intHexadecimal = 0x10 // 十六进制
//浮点数
let doubleDecimal = 314.15 // 十进制,等价于 3.1415e2
let doubleDecimal1 = 3.1415e2
let doubleHexadecimal1 = 0xFp2 // 十六进制,意味着15x2^2,相当于十进制的 60.0
let doubleHexadecimal2 = 0xFp-2 // 十六进制,意味着15x2^-2,相当于十进制的3.75
//以下都是表示 3.1875
//十进制:3.1875、0.31875e1
//十六进制:0x3.3p0
整数和浮点数可以添加额外的零或者添加下划线来增强可读性
100_0000、1_000_000.000_0001、000789.245
let array = [1,2,3,4,5,6,8,9]
let dictionary = ["width" : 30, "height" : 40, "depth": 50]
//整数转换
let int1: UInt16 = 1_000
let int2: UInt8 = 2
let int3 = int1 + UInt16(int2)
//整数、浮点数转换
let int = 4
let double = 3.14159
let num = Double(int) + double
let intNum = Int(num)
字面量可以直接相加,因为数字字面量本身没有明确的类型
let rs = 3 + 1.14159
let http404Error = (404, "Not Found")
print("The status code is \(http404Error.0)")
let (statusCode, StatusMsg) = http404Error
print("The status code is \(statusCode)")
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
let http200Status = (statusCode:200,des:"OK")
print("The status code is \(http200Status.statusCode)")