When should I import Foundation into my Swift source file?

When should I import Foundationin the Swift source file?

Xcode adds it to the default template, but I was wondering when you really need to import it.

+6
source share
2 answers

If you look at the Foundation Framework Reference , you can see which part of it. In particular, if you are not using any subclasses of NSObject (NSString, NSArray, etc.), you can probably remove this.

If you use Objective-C, you probably need to use Foundation, but if your code is mostly Swift, you can simply delete it, since String, Array are not subclasses of NSObject, but part of the Swift Standard Library.

+6
source

A framework is a framework containing several APIs (NSDate, NSString, NSDateFormatter). If you need some of its functions, you can import them; if not, then this is not required. And if you use UIKit, Foundation is already implemented in it, so you do not need to import it twice.

+1
source

Source: https://habr.com/ru/post/1607115/


All Articles