swift学习

Swift5.1 入门(1)

2019-06-20  本文已影响44人  HChase

1. 常量和变量

var 定义变量,let 定义常量,系统自动推断出变量或常量类型;

    func test() {
        var a = 52.0
        let b = 10
        
        a = a + Double(b)
        print("\(a)  \(b)")
    }
    func test1() {
        let b: Int
        b = 40
        print(b)
    }

2. 标识符

    func 😩🐂() {
        let 😊: Int = 10
        var 🧨: Float = 12.0
        🧨 = 40.0
        
        print(😊)
        print(🧨)
    }

3. 常见的数据类型


4. 字面量

let b = true
let b = fase

let a = "abc123"

let a: Character = "a"
let a: Character = "🐶"

let a = 10
let a: Int = 10   //十进制
let a = 0b1001   //二进制
let a = 0o34   //八进制
let a =0xff   //十六进制

let a = 12.8   //十进制
let a = 0xfp2   //十六进制,等价于十进制15 * 2^2 = 60.0
let a = 0xfp-2   //十六进制,等价于十进制15 * 2^-2 = 3.75

let a = [1,2,3,4,5]

let a = ["age": 20, "height": 170]


5. 类型转换

        let a: Int = 20
        let b: Float = Float(a)
        let a: Int = 20
        let b: Float = 30.0
        let c: Float = b + Float(a)

        // 字面量可以直接相加,因为数字字面量本身没有明确的类型
        let d = 5 + 1.67

6. 元组(Tuple)

元组的几种定义方式:

        // 方式1
        let httpStatus = (200, "request success")
        print("\(httpStatus.0)")

        // 方式2
        let (statusCode, response) = (200, "request success")
        print("\(statusCode)   \(response)")

        // 方式3
        let (statusCode, _) = (200, "request success")
        print("\(statusCode) ")

        // 方式4
        let httpStatus = (statusCode: 200, response: "request success")
        print("\(httpStatus.statusCode)   \(httpStatus.response)")

7. swift的编译过程
swift 编译过程

swift的编译过程:


  1. swift code : swift 源代码;
  2. swift ast : 将swift 源代码生成语法树 ;
swiftc-dump-ast    main.swift
  1. raw Swift IL: 生成最简洁的SIL代码;
swiftc-emit-sil    main.swift
  1. 生成 llvm IR 代码;
swiftc-emit-ir   main.swift  -o main.ll
  1. 生成汇编代码
swiftc-emit-assembly   main.swift  -o main.s
上一篇下一篇

猜你喜欢

热点阅读