Should I include import for Foundation while importing UIKit (which has Foundation import in itself)?
Could UIKit work without Foundation in the future and in theory break my code down the road?
CodePudding user response:
Always import the lowest level you can get away with:
If your file is pure library Swift, import nothing.
If your file needs Foundation types, import Foundation.
If your file needs UIKit types (they all start with
UI
), import UIKit.If your file needs SwiftUI types, import SwiftUI.
You should do exactly one of the above. As for your original question, UIKit itself imports Foundation (as you have rightly said). Therefore if a file imports UIKit, it does not need to import Foundation explicitly, and you should not import it explicitly.
UIKit will not magically lose its ability to access Foundation types in the future. UIKit without, say, NSString would be a metaphysical impossibility. Conversely, if NSString went away, UIKit itself would go away and that would be the breakage.
CodePudding user response:
No you only need to import Foundation for classes that don't use UIKit, its possible you want to use the classes that import Foundation with SwiftUI, or AppKit in the future, so its best to keep your UI code seperate from you none UI code, I personal won't even let UIImage or UIColor in viewModels, because as I see it viewModels should be FoundationKit only.
CodePudding user response:
No, you do not need to import both Foundation and UIKit. UIKit is enough if you use any UI* types. If you do not use any UI* types, you do not need UIKit and can leave only Foundation.