This code is working good in 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
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"!
Playground's bug?
When importing Foundation (or anything else like Cocoa or UIKit that will import it implicitly) Swift automatically converts some Objective-C types to Swift types, and some Swift types to Objective-C types, and a number of data types in Swift and Objective-C can be used interchangeably.
You need import Foundation if you are using things like Date or AttributedString or other APIs that aren't a part of the Swift Standard Library or a specialized framework (see below).
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.
The Foundation framework defines a base layer of Objective-C classes. In addition to providing a set of useful primitive object classes, it introduces several paradigms that define functionality not covered by the Objective-C language. The Foundation framework is designed with these goals in mind −
Your playground is most likely created for the iOS platform - Cocoa
is 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:
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.
However, the code you posted in your question uses classes from Foundation only, so there's no need to import either UIKit or Cocoa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With