Reputation:
I have been learning Swift and had a recurring problem throughout. The playground, when run, doesn't finish running the code, even the default MyPlayground file. I get no output whatsoever.
I have searched online and others have the same problem as me but no answer. This happens for the default and built up files I have created previously.
I spoke to Apple on 3 separate occasions and got nothing and referred to the Developer forums and they haven't got an answer either.
Any ideas guys?
For example,
//: Playground - noun: a place where people can play
import Cocoa
var str = "Hello, playground"
print(str)
This is the default and when run, I don't get the output of str or anything in the Utilities view, it just says running MyPlayground at the top.
Thanks
Upvotes: 4
Views: 2345
Reputation: 6024
What are you building for? iOS, macOS, or tvOS?
The default file for macOS is as you say:
import Cocoa
var str = "Hello, playground"
Which runs perfectly, with no errors.
But when I run your code built for iOS
, Xcode throws an error:
Swift Compiler Warning: No such module `Cocoa`.
Either way, you cannot import Cocoa
in playgrounds built for iOS, so don't import Cocoa
, import UIKit
instead. Besides, import UIKit
is the default file when building for iOS. So I suspect you're running the default macOS file in an iOS build of playgrounds.
There is another question here which addresses the issue of importing Cocoa
in Playgrounds.
Since you're having what looks like a null pointer exception, based on your comment, likely from the project trying to load a non-existent object, here are some troubleshooting steps:
import Cocoa
.import
(Notice the space at the end.)C
C
doesn't come up with an autocomplete list with Cocoa
in it, then it's not part of the build.Next, in the same playground:
import Cocoa
lineimport
(space at the end.)UI
and wait for an autocomplete listUI
has the autocomplete option for UIKit, then Cocoa isn't part of the playground.Upvotes: 1