import Swift vs import Foundation import Swift vs import Foundation ios ios

import Swift vs import Foundation


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.


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/


import Foundation used for access NSObject or one of its sub class.NSObject means we can extend our class using objective C runtime features.But import UIKit or import swift, that is automatically generate while we create our xcode project