Swift基本语法

2020-09-30  本文已影响0人  得_道

概览

常量
标识符
常见数据类型
字面量
类型转换
元祖

常量

let age1 = 10

let age2: Int
age2 = 20

func getAge() -> Int {
    return 30
}

let age3 = getAge()

标识符

func 🐂🍺() {
    print("6666")
}

🐂🍺()

let 👽 = "ET"
let 🥛 = "milk"

常见数据类型

image.png
let letFloat: Float = 20.0
let letDouble = 20.0

字面量

//布尔
let bool = true

//字符串
let string = "张三丰"

//字符(可存储ASCII字符、Unicode字符)
let character: Character = "🐂"

//整数
let intDecimal = 66

//浮点数
let doubleDecimal = 66.0

//数组
let array = [1,3,5,7,9]

//字典
let dictionary = ["age":18, "height":168]

类型转换

整数转换

let int1: UInt16 = 2_000
let int2: UInt8 = 1
let int3 = int1 + UInt16(int2)

整数、浮点数转换

let int = 3
let double = 0.14159
let pi = Double(int) + double
let intPi = Int(pi)

字面量可以直接相加,因为字面量本身没有明确的类型

let result = 3 + 0.14159

元祖

let http404Error = (404, "not found")
print("the status code is \(http404Error.0)")

let http200Status = (statusCode:200, description: "OK")
print("the status code is \(http200Status.statusCode)")

let (statusCode, statusMessage) = http404Error
print("the status code is\(statusCode)")

let (justTheStatusCode, _) = http404Error
上一篇下一篇

猜你喜欢

热点阅读