gpt4 book ai didi

ios - 在我们的 Swift 文件中,我在哪里导入 swift 模块?

转载 作者:行者123 更新时间:2023-11-30 12:48:29 25 4
gpt4 key购买 nike

我正在阅读 Matt 的 iOS 编程书,当我读到这里时:

Swift itself is defined in a module — the Swift module. Your code always implicitly imports the Swift module. You could make this explicit by starting a file with the line import Swift; there is no need to do this, but it does no harm either.

That fact is important, because it solves a major mystery: where do things like print come from, and why is it possible to use them outside of any message to any object? print is in fact a function declared at the top level of the Swift module, and your code can see the Swift module’s top-level declarations because it imports Swift. The print function thus becomes, as far as your code is concerned, an ordinary top-level func‐ tion like any other; it is global to your code, and your code can speak of it without specifying its namespace. You can specify its namespace — it is perfectly legal to say things like Swift.print("hello") — but you probably never will, because there’s no name conflict to resolve.

例如,在我的测试中ViewController:

import UIKit
//import Swift // here we can import Swift. it is okay

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

Swift.print("hello, matt") // where we import the Swift ?

}
}

我们可以使用printSwift.print,我只是想问,apple如何隐式导入Swift模块?很明显,UIKit,我们必须导入,如果可以隐式导入Swift模块,为什么苹果不隐式导入UIKit

我的困惑是如何隐式导入 Swift 模块?

最佳答案

Swift 模块的隐式导入是一种硬编码到 Swift 编译器中的行为,专门用于 Swift 模块。

关于ios - 在我们的 Swift 文件中,我在哪里导入 swift 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com