07-18-2023, 03:50 PM
**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]:
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]