fabmilo
fabmilo

Reputation: 48330

When Should I import Foundation in a Swift source file?

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.

Upvotes: 26

Views: 8977

Answers (2)

Kristijan Delivuk
Kristijan Delivuk

Reputation: 1252

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.

Upvotes: 11

fdiaz
fdiaz

Reputation: 2600

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.

Upvotes: 20

Related Questions