swiftxcodecocoafoundation

what is the difference between 'import Cocoa' and 'import Foundation' in Xcode's Playground


This code is working well 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))")

The only difference is the line "import Cocoa"!

Is it a bug in Playground?


Solution

  • 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.