My technology blog
let a = ["1", "2"]

func doSomething1() {
    print(a)
}

doSomething1()

let b = doSomething1()

func doSomething2() -> Int {
    return 1
}

你永远无法调用一个定义在下面的函数,就如同b无法调用doSomething2

同理structclass也不会提升

let a = S1()

struct S1 {
    var name = 1
}

但是我们把他们放到两个文件中就可以运行

// File1.swift
let a = S1()
// File2.swift
struct S1 {
    var name = 1
}

修改结构体,使其需要a的值

struct S1 {
    var name = 1
    var word = a
}

于是,虽然在代码提示中a可以赋值为S1(),但是却不能用点语法调用属性,就算只是赋值S1,也会报错。

总的来说,Swift会通过代码生成合适的调用方式,如果在一个文件中那么就是顺序调用,如果在不同文件中,调用方式则由具体代码觉得。
You’ve successfully subscribed to UTS Blog
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Your link has expired
Success! Check your email for magic link to sign-in.