user7536421
user7536421

Reputation:

Playground Xcode never stops running, any ideas?

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

Answers (1)

NonCreature0714
NonCreature0714

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:

  1. Erase import Cocoa.
  2. Type in import (Notice the space at the end.)
  3. Type in C
  4. If C doesn't come up with an autocomplete list with Cocoa in it, then it's not part of the build.
  5. And this would explain the null pointer exception (EXC_BAD_ACCESS at 0x0.)

Next, in the same playground:

  1. Erase the import Cocoa line
  2. Type in import (space at the end.)
  3. Type in UI and wait for an autocomplete list
  4. If UI has the autocomplete option for UIKit, then Cocoa isn't part of the playground.
  5. Which is why there is a null pointer error.

Upvotes: 1

Related Questions