45
votes

I'm trying to use the standard Cocoa library in a swift file in Xcode 6 Beta. I followed this instructions but when I import the library

import Cocoa

XCode complains with the error

No such module 'Cocoa'

I also tried with the REPL and I have no problems at all. I suppose it's a bug, cause I started different projects and only sometimes I get this error. Any suggestions? I'm using Mavericks (10.9.3)

7

7 Answers

123
votes

Replace

import Cocoa

With:

import UIKit
39
votes

You can't import Cocoa from an iOS playground or application. Make sure your code is running in a Cocoa playground (select OS X > Source in the new file dialog).

22
votes

From your question, it seems that you are trying to add Cocoa class into your iOS Application instead of Cocoa Touch class. So this is not a bug of Xcode.

Be sure to select template which are under the iOS section shown in the following screenshot, as we are choosing it for an iOS Application.

enter image description here

8
votes

If you've already created your Playground, you can switch between iOS and OS X platforms in the "File Inspector" dialog; there's no need to create a new Playground. import Cocoa will only work for an OS X Playground.

Go to View > Utilities > Show File Inspector and then it's on the right under "Playground Settings"

2
votes

you need to get xcode 6.1 (beta) to use Swift with OS X

2
votes

You can open the Utilities tab (from the top right) and just switch platform from the Playground settings section. If you switch it from iOS to OS X the problem will be resolved.

0
votes

Framework named Cocoa does not exists

Remove

import Cocoa

and everything must works from the scratch.

If this does not work for some reason you can import Foundation or UIKit frameworks