Xcode 的 Playground 中的“import Cocoa”和“import Foundation”有什么不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27043853/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:13:31  来源:igfitidea点击:

what is differant 'import Cocoa' and 'import Foundation' in Xcode's Playground

xcodeswift

提问by dialektike

This code is working good in Playground

此代码在 Playground 中运行良好

import Foundation

let stringDate : NSString = "1403437865"
let date = NSDate(timeIntervalSince1970:stringDate.doubleValue)

var outputFormat = NSDateFormatter()
outputFormat.locale = NSLocale(localeIdentifier:"ko_KR")
outputFormat.dateStyle = .MediumStyle
outputFormat.timeStyle = .MediumStyle
println("Result: \(outputFormat.stringFromDate(date))")

but this code is not working in Playground

但此代码在 Playground 中不起作用

import Cocoa

let stringDate : NSString = "1403437865"
let date = NSDate(timeIntervalSince1970:stringDate.doubleValue)

var outputFormat = NSDateFormatter()
outputFormat.locale = NSLocale(localeIdentifier:"ko_KR")
outputFormat.dateStyle = .MediumStyle
outputFormat.timeStyle = .MediumStyle
println("Result: \(outputFormat.stringFromDate(date))")

only different 1 line "import Cocoa"!

只有不同的 1 行“进口可可”!

Playground's bug?

游乐场的bug?

采纳答案by Antonio

Your playground is most likely created for the iOS platform - Cocoais a framework for the OS X target, and its iOS counterpart is UIKit, and both contain user interface related APIs (for the respective platform). Try changing that to:

您的 Playground 很可能是为 iOS 平台创建的 -Cocoa是 OS X 目标的框架,它的 iOS 对应物是UIKit,并且都包含与用户界面相关的 API(针对相应平台)。尝试将其更改为:

import UIKit

and it should work.

它应该工作。

Foundation is a framework containing several APIs, such as NSString, NSDate, NSDateFormatter. It is already included in Cocoa and UIKit, so you don't need to reimport if already importing one of the 2.

Foundation 是一个包含多个 API 的框架,例如 NSString、NSDate、NSDateFormatter。它已包含在 Cocoa 和 UIKit 中,因此如果已经导入了 2 个中的一个,则无需重新导入。

However, the code you posted in your question uses classes from Foundation only, so there's no need to import either UIKit or Cocoa.

但是,您在问题中发布的代码仅使用 Foundation 中的类,因此无需导入 UIKit 或 Cocoa。