我应该在导入 UKit 的同时导入 Foundation 吗?
我应该在导入 UIKit (它本身具有 Foundation 导入)时包含 Foundation 的导入吗?
UIKit 将来可以在没有 Foundation 的情况下工作吗?理论上可以破坏我的代码吗?
Should I include import for Foundation while importing UIKit (which has Foundation import in itself)?
Could UIKit work without Foundation in the future and in theory break my code down the road?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
始终导入您可以逃脱的最低级别:
如果您的文件是纯 Swift 库,则不导入任何内容。
如果您的文件需要 Foundation 类型,请导入 Foundation。
如果您的文件需要 UIKit 类型(它们都以
UI
开头),请导入 UIKit。如果您的文件需要 SwiftUI 类型,请导入 SwiftUI。
您应该完全执行上述操作中的一个。至于你原来的问题,UIKit 本身导入了 Foundation(正如你所说的那样)。因此,如果文件导入 UIKit,则不需要显式导入 Foundation,并且您不应该显式导入它。
UIKit 将来不会神奇地失去访问 Foundation 类型的能力。比如说,如果 UIKit 没有 NSString,那将是形而上学的不可能。相反,如果 NSString 消失,UIKit 本身也会消失,这将是损坏。
Always import the lowest level you can get away with:
If your file is pure library Swift, import nothing.
If your file needs Foundation types, import Foundation.
If your file needs UIKit types (they all start with
UI
), import UIKit.If your file needs SwiftUI types, import SwiftUI.
You should do exactly one of the above. As for your original question, UIKit itself imports Foundation (as you have rightly said). Therefore if a file imports UIKit, it does not need to import Foundation explicitly, and you should not import it explicitly.
UIKit will not magically lose its ability to access Foundation types in the future. UIKit without, say, NSString would be a metaphysical impossibility. Conversely, if NSString went away, UIKit itself would go away and that would be the breakage.
不,您不需要同时导入 Foundation 和 UIKit。
如果您使用任何 UI* 类型,UIKit 就足够了。
如果您不使用任何 UI* 类型,则不需要 UIKit,可以只保留 Foundation。
No, you do not need to import both Foundation and UIKit.
UIKit is enough if you use any UI* types.
If you do not use any UI* types, you do not need UIKit and can leave only Foundation.
不,您只需要为不使用 UIKit 的类导入 Foundation。
您将来可能希望使用通过 SwiftUI 或 AppKit 导入 Foundation 的类,因此最好将 UI 代码与非 UI 代码分开。
我个人甚至不会在视图模型中使用
UIImage
或UIColor
,因为我认为视图模型应该仅是 Foundation 的。No you only need to import Foundation for classes that don't use UIKit.
It's possible you will want to use the classes that import Foundation with SwiftUI or AppKit in the future, so it's best to keep your UI code separate from you non-UI code.
I personally won't even use
UIImage
orUIColor
in view models, because as I think view models should be Foundation only.UIKit 更有可能在 Foundation 之前变得多余,SwiftUI 已经成为 UIKit 的替代品,而且 Foundation 比 UIKit 更通用,例如,如果你有一些只需要 Foundation 的东西,它就可以在 UIKit 应用程序中工作, SwiftUI 应用程序、MacOS ApplicationKit 应用程序、TVOS 应用程序、没有 GUI 的 Comandline 工具。
UIKit is much more likely to be made redundant before Foundation, SwiftUI is already becoming the replacement for UIKit, and Foundation is much more general than UIKit, for example if you have something that only needs foundation, it can potential work in a UIKit application, a SwiftUI application, a MacOS ApplicationKit application, a TVOS application, a Comandline tool that has no GUI.