如何避免在每个swift文件上导入相同的Framework [英] how to avoid importing the same Framework on every swift file
问题描述
有没有办法在每个文件上始终导入几个库?
而不将它们放入框架
Is there a way to always import a few libraries on every file? without putting them into a framework
例如,而不是必须
- MySwiftFile.swift--
--MySwiftFile.swift--
import UIKit
import Foundation
import ...
我可以这样做:
- SharedImports.swift -
--SharedImports.swift--
import UIKit
import Foundation
import ...
- MySwiftFile.swift--
--MySwiftFile.swift--
import SharedImports.swift
推荐答案
答案是否 ,这是有目的的。每个文件都需要知道该文件中包含的代码的上下文。它从导入集中获取该上下文。
The answer is "No, and that's on purpose". Each file needs to know the context of the code that is contained within that file. It gets that context from the set of imports.
现在在UIKit和Foundation的特定情况下,应该是 UIKit的情况
imports Foundation
所以我认为你不必在每个文件中都明确地调用它们。在上面的示例中,您应该可以使用
Now in the particular case of UIKit and Foundation, it should be the case that UIKit
imports Foundation
so I don't think you have to explicitly call out both in every file. In the examples above you should be able to get by with just
import UIKit
有时,在定义应用程序的模型时,您可能希望文件带入 Foundation
而不是 UIKit
。
There are times, when defining your application's model for example, where you may want a file to bring in Foundation
and not UIKit
.
这篇关于如何避免在每个swift文件上导入相同的Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!