Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Should I import Foundation in a Swift source file?

Tags:

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.

like image 303
fabrizioM Avatar asked Sep 12 '15 21:09

fabrizioM


2 Answers

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.

like image 77
fdiaz Avatar answered Sep 16 '22 13:09

fdiaz


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.

like image 42
Kristijan Delivuk Avatar answered Sep 16 '22 13:09

Kristijan Delivuk