Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 193 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import Swift vs import Foundation

#1
**Question**

What is the difference between `import Swift` and `import Foundation`?

Until I read [this comment][1] by [Martin R][2], 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?


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#2
*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
Reply

#3
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:-

[To see links please register here]


Pleases refer:-

[To see links please register here]

Reply

#4
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][1].


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through