No such module 'Cocoa' in Swift Playground

2020-02-17 06:26发布

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.

3条回答
Root(大扎)
2楼-- · 2020-02-17 06:57

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.

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-17 07:23

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
查看更多
家丑人穷心不美
4楼-- · 2020-02-17 07:23

This answer helped me:

https://forums.bignerdranch.com/t/import-cocoa-in-playground-is-giving-error/8091/2

I fixed it… never replace an iOS playground by a OS X playground when you name them the same… it’ll stick with the iOS mode…

查看更多
登录 后发表回答