46

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)

BergQuester
  • 6,167
  • 27
  • 39
Griffosx
  • 1,605
  • 1
  • 16
  • 23

7 Answers7

124

Replace

import Cocoa

With:

import UIKit
Robert
  • 37,670
  • 37
  • 171
  • 213
  • does anyone know if there's a way to set this to be the default with new files? I have to do this for every file I create. – Matthew Knippen Sep 26 '15 at 20:25
  • 1
    @MatthewKnippen - It seems to be a bug in Xcode, it should know you are doing iOS not OS X development. File a radar (but actually don't since I am getting a lot of rep out of this) – Robert Sep 27 '15 at 11:34
  • 2
    it turns out that you can specify when you create a new file. When you do, it keeps the new default. – Matthew Knippen Sep 28 '15 at 04:46
  • 1
    of course, if you switch between OS X and iOS programming a lot, then this will still be a problem – Matthew Knippen Sep 28 '15 at 04:47
39

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

nathan
  • 5,466
  • 3
  • 27
  • 24
22

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

NSPratik
  • 4,714
  • 7
  • 51
  • 81
8

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"

Matthew
  • 1,366
  • 2
  • 15
  • 28
2

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

ZZB
  • 790
  • 6
  • 11
2

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.

KOstas
  • 79
  • 1
  • 10
0

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

Sergey Zhukov
  • 714
  • 6
  • 5