how to avoid importing the same Framework on every

2019-06-18 04:10发布

问题:

Is there a way to always import a few libraries on every file? without putting them into a framework

for example, instead of having to do

--MySwiftFile.swift--

import UIKit 
import Foundation
import ...

I could do something like:

--SharedImports.swift--

import UIKit 
import Foundation
import ...

--MySwiftFile.swift--

import SharedImports.swift

回答1:

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.

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

There are times, when defining your application's model for example, where you may want a file to bring in Foundation and not UIKit.