Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

swift

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?

like image 713
dialektike Avatar asked Nov 20 '14 15:11

dialektike


People also ask

Why we use import Foundation in Swift?

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.

When should you import foundation?

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

What is UIKit and foundation in Swift?

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.

What is foundation h in Objective-C?

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 −


1 Answers

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.

like image 130
Antonio Avatar answered Oct 12 '22 11:10

Antonio