Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No such module 'Cocoa' in Swift Playground

I'm trying to follow some of the code used in the WWDC playgrounds session, I'm trying to import the Cocoa framework with:

import Cocoa 

But I get the following error in the assistant editor

Playground execution failed: error: <REPL>:3:8: error: no such module 'Cocoa' 

I'm at 41:20 in the video if that helps at all.

like image 954
Charlie Egan Avatar asked Jun 07 '14 16:06

Charlie Egan


People also ask

What is cocoa in Swift?

Cocoa is a set of object-oriented frameworks that provides a runtime environment for applications running in OS X and iOS. Cocoa is the preeminent application environment for OS X and the only application environment for iOS.

What is the difference between Swift playground and Xcode?

One of the biggest differences between Swift Playgrounds and Xcode Playgrounds is that Swift Playgrounds are much less powerful and are built more as an educational tool. My biggest fear is that as Apple brings Swift Playgrounds to the Mac that they will stop supporting and growing Xcode Playgrounds.

What is variable in Swift playground?

They are called variables because they can vary – you can change their values freely. Playgrounds start with a line of code that creates a variable for us: var str = "Hello, playground" That creates a new variable called str , giving it the value “Hello, playground”.


2 Answers

You are using an iOS playground (UIKit-based), not an OS X playground (Cocoa-based). Try creating a new playground and choosing "OS X" as the type instead of "iOS". It should work fine after that.

You can also change the type for an existing playground in the File Inspector (View→Inspectors→Show File Inspector) under Playground Settings→Platform.

By default, new iOS playgrounds are created with boilerplate including import UIKit; OS X playgrounds are created with boilerplate including import Cocoa, so if you find yourself manually typing "import Cocoa", it's probably a clue you've got the wrong type.

like image 178
Matt Gibson Avatar answered Sep 26 '22 01:09

Matt Gibson


If you are getting this or a similar type of error despite having the playground set properly, try the following:

Close XCode and from the terminal run:

 rm -rf "$(getconf DARWIN_USER_CACHE_DIR)/org.llvm.clang/ModuleCache" 

Then reopen XCode and most times this fixes many Playground issues. I run this command quite frequently during an intensive Playground work session (sometimes as many as a few times per hour) and have found liberal use of removing the ModuleCache to solve a multitude of strange playground behaviors.

If you're still having issues with playgrounds you can try:

rm -rf ~/Library/Developer/Xcode/DerivedData 

and/or

rm -rf ~/Library/Caches/com.apple.dt.Xcode 
like image 32
Jay Avatar answered Sep 26 '22 01:09

Jay