ios 导入 Swift 与导入基础
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33943477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
import Swift vs import Foundation
提问by Suragch
Question
题
What is the difference between import Swift
and import Foundation
?
import Swift
和 和有import Foundation
什么区别?
Until I read this commentby Martin R, I didn't even know that there was an import Swift
.
直到我读这条评论被马丁- [R ,我甚至不知道有一个import Swift
。
Reading
读
I couldn't find the documentation and doing a Google search didn't turn up much.
我找不到文档,而且在 Google 上搜索也没有找到太多。
What I tried
我试过的
Testing it out shows that import Swift
does not give any compile errors, but that doesn't really answer my question.
测试表明import Swift
它没有给出任何编译错误,但这并不能真正回答我的问题。
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
).
如果我猜的话,我会说您为 Swift 项目导入 Swift,为 Objective-C 项目或可能为使用 Objective-C 类的 Swift 项目(如NSString
)导入 Foundation 。
Testing this in the Playground:
在 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
.
注释掉import Swift
没有错误并且str
是String
类型的。但是,注释掉import Foundation
会为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?
我很乐意放弃 Foundation 而只使用 Swift。那么,除非我特别需要使用旧的 Objective-C 类之一,否则我一直只导入 Swift 是否正确?
采纳答案by Sidney
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.
是的,仅import Foundation
当您想访问 NSObject 或其子类之一时才需要。Foundation 是引入该类层次结构的框架。但是,很可能在一个项目中,您需要的不仅仅是import Swift
. 就像 Rob 评论的那样,import UIKit
也是一个不错的选择。
In case you haven't read it already, Apple explains the Foundation framework here.
如果您还没有阅读它,Apple 在此处解释了 Foundation 框架。
回答by arango_86
If you want to work with Strings, Dates, etc you need to import Foundation.
如果您想使用字符串、日期等,您需要导入 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.
Foundation 框架为应用程序和框架提供基础功能层,包括数据存储和持久化、文本处理、日期和时间计算、排序和过滤以及网络。
If you want to work with UITableViewController, UIAlertController you need to import UIKit.
如果你想使用 UITableViewController, UIAlertController 你需要导入 UIKit。
If you import UIKit you do not need to import Foundation because it already imports it in the backstage.
如果导入 UIKit,则不需要导入 Foundation,因为它已经在后台导入了。
The Swift standard library defines a base layer of functionality for writing Swift programs, including:
Swift 标准库定义了用于编写 Swift 程序的基础功能层,包括:
Fundamental data types, Common data structures, Global functions such as print(:separator:terminator:) and abs(:), Protocols, such as Collection and Equatable... etc
基本数据类型、通用数据结构、全局函数如 print( :separator:terminator:) 和 abs(:)、协议,如 Collection 和 Equatable...等
If you import Foundation, then no need to import Swift again as Foundation contains references to Swift Standard Library by default.
如果您导入 Foundation,则无需再次导入 Swift,因为 Foundation 默认包含对 Swift 标准库的引用。
When you are writing something not for iOS Apps, like say a server programming based on Vapor , you may need to consider import Swift.
当你写一些不是为 iOS 应用程序编写的东西时,比如基于 Vapor 的服务器编程,你可能需要考虑导入 Swift。
Refer:- https://developer.apple.com/documentation/swift/swift_standard_library/
参考:- https://developer.apple.com/documentation/swift/swift_standard_library/
Pleases refer:- https://hasancan.tech.blog/2018/01/17/import-foundation-vs-uikit/
请参考:- https://hasancan.tech.blog/2018/01/17/import-foundation-vs-uikit/
回答by kurupareshan
import Foundationused for access NSObject or one of its sub class.NSObject means we can extend our class using objective Cruntime features.But import UIKit or import swift, that is automatically generate while we create our xcode project
import Foundation用于访问 NSObject 或其子类之一。NSObject 意味着我们可以使用目标 C运行时功能扩展我们的类。但是import UIKit 或 import swift,这是在我们创建 xcode 项目时自动生成的