Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import Swift vs import Foundation

Question

What is the difference between import Swift and import Foundation?

Until I read this comment by Martin R, I didn't even know that there was an import Swift.

Reading

I couldn't find the documentation and doing a Google search didn't turn up much.

What I tried

Testing it out shows that import Swift does not give any compile errors, but that doesn't really answer my question.

If I were to guess, I would say that you import Swift for Swift projects and that you import Foundation for Objective-C projects or maybe for Swift projects that use Objective-C classes (like NSString).

Testing this in the Playground:

import Foundation import Swift  var str = "Hello, playground" let str2: NSString = "hello" let str3: String = "hello" 

Commenting out import Swift gives no errors and str is of String type. However, commenting out import Foundation gives an "undeclared type" error for NSString.

My question revisited

I would be happy enough to abandon Foundation and just use Swift. So am I right to just import Swift all the time unless I specifically need to use one of the old Objective-C classes?

like image 345
Suragch Avatar asked Nov 26 '15 16:11

Suragch


People also ask

What is the use of import foundation in Swift?

When importing Foundation (or anything else like Cocoa or UIKit that will import it implicitly) Swift automatically converts some Objective-C types to Swift types, and some Swift types to Objective-C types, and a number of data types in Swift and Objective-C can be used interchangeably.

What is foundation Swift?

Foundation. The Foundation framework defines a base layer of functionality that is required for almost all applications. It provides primitive classes and introduces several paradigms that define functionality not provided by the language or runtime.

Do you need to import foundation and UIKit?

No, you do not need to import both Foundation and UIKit. UIKit is enough if you use any UI* types.

What is UIKit and foundation in Swift?

UIKit is an "umbrella" framework, which imports a lot of other frameworks, including Foundation, so it really is imported. There's actually more to this than meets the eye. As it happens, Swift contains a re-implementation of the NSString APIs within the String class.


2 Answers

Yes, you will only need import Foundation if you want to access NSObject or one of its subclasses. Foundation is the framework that brings in that class hierarchy. However, it's highly likely that in a project you'll need more than just import Swift. Like Rob commented, import UIKit is also a nice option.

In case you haven't read it already, Apple explains the Foundation framework here.

like image 89
Sidney Avatar answered Sep 30 '22 22:09

Sidney


If you want to work with Strings, Dates, etc you need to import Foundation.

The Foundation framework provides a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

If you want to work with UITableViewController, UIAlertController you need to import UIKit.

If you import UIKit you do not need to import Foundation because it already imports it in the backstage.

The Swift standard library defines a base layer of functionality for writing Swift programs, including:

Fundamental data types, Common data structures, Global functions such as print(:separator:terminator:) and abs(:), Protocols, such as Collection and Equatable... etc

If you import Foundation, then no need to import Swift again as Foundation contains references to Swift Standard Library by default.

When you are writing something not for iOS Apps, like say a server programming based on Vapor , you may need to consider import Swift.

Refer:- https://developer.apple.com/documentation/swift/swift_standard_library/

Pleases refer:- https://hasancan.tech.blog/2018/01/17/import-foundation-vs-uikit/

like image 22
arango_86 Avatar answered Sep 30 '22 21:09

arango_86