When Should I import Foundation
in a Swift source file?
Xcode adds it in the default Template, but I was wondering when it is really required to import it.
If you take a look at the Foundation Framework Reference you can see what's part of it. Particularly if you're not using anything that subclasses NSObject (NSString, NSArray, etc.), you probably can remove that.
If you're using Objective-C, you'll probably need to use Foundation, but if your code is mainly Swift, you can just remove it since String, Array, are not subclasses of NSObject but part of the Swift Standard Library.
Foundation is a framework containing several APIs (Date, String, DateFormatter). If you need some of its functionalities you should import it. If you don't need it, it is not required.
If you're using UIKit, Foundation is already implemented in it so you don't need to import it twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With