如何避免在每个 swift 文件上导入相同的框架

how to avoid importing the same Framework on every swift file

有没有办法总是在每个文件上导入几个库? 没有将它们放入框架

例如,不必做

--MySwiftFile.swift--

import UIKit 
import Foundation
import ...

我可以这样做:

--SharedImports.swift--

import UIKit 
import Foundation
import ...

--MySwiftFile.swift--

import SharedImports.swift

答案是"No, and that's on purpose"。每个文件都需要知道该文件中包含的代码的上下文。它从一组导入中获取上下文。

现在在 UIKit 和 Foundation 的特定情况下,UIKit 导入 Foundation 应该是这种情况,所以我认为您不必在每个文件中都显式调用。在上面的示例中,您应该能够通过

import UIKit

有时候,例如在定义应用程序的模型时,您可能希望文件引入 Foundation 而不是 UIKit