4

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

  • I wouldn't know, but have you tried reinstalling Xcode? – Cullub Feb 08 '17 at 19:27
  • Interesting question. Playing devil's advocate, what exactly *is* "stopping running"? Once an iOS app is loaded, it may move into the background. It may even be forced-shutdown by the user. But *stopped*? Not really. –  Feb 08 '17 at 19:28
  • Breaking this into a new comment - can you give us a concrete, specific, repeatable example? One with a behavior we can repeat, along with how you think it should work. –  Feb 08 '17 at 19:29
  • Yeh, I've wiped my laptop clean and everything today to try to fix it too, it's just really tedious. –  Feb 08 '17 at 19:29
  • I've added a little to explain my problem. Not stopped but finished running the code. –  Feb 08 '17 at 19:33
  • Getting back to my first question - why *should* it end? If you move the playground app into the background I'd expect it to, well, move into the background :-) but once you move it into the foreground, it shouldn't need to "reload" your code (or the playground code) unless the app(s) you've used wanted the memory allocated. That's the nature of an iOS app. EDIT: Reading your edit, are you saying "Hello, playground" doesn't display on the right side of things? It would in the console session in a *full* Xcode session, so maybe that is a behavior of the playground app. –  Feb 08 '17 at 19:46
  • Sorry, I understand that, I'm only testing out code in the playgrounds at the moment but when you execute the playground, it never gives me any output from the code whatsoever, regardless of the complexity. –  Feb 08 '17 at 19:49
  • Ah, didn't see the edit. It was working fine for a good few days and then it started to creep in. –  Feb 08 '17 at 19:50
  • I guess I don't have an answer that helps, and I expect the SO bot is about to ask we go to chat. The only helpful thought I have is to ask that you edit your question to make it clear if you are speaking of a "playground" in the Xcode app run on a Mac or speaking of a playground running in the "playground" app on iPad. (Either way, I'm not sure why your simple code isn't working in a playground.) Good luck getting an answer! I'll monitor and remove some comments soon. Don't want to clutter up this question. –  Feb 08 '17 at 19:55

1 Answers1

1

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.
Community
  • 1
  • 1
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
  • I created a new playground to make sure it was in macOS because I just assumed after the first one it would have taken macOS as the default for new files. I have the code me and you have agreed on and it doesn't run perfectly.. –  Feb 09 '17 at 00:23
  • 1
    error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. –  Feb 09 '17 at 00:24
  • @Bowcox What year/model is your mac? – NonCreature0714 Feb 09 '17 at 00:26
  • @Bowcox judging by your error message, Xcode is trying to dereference a null pointer. What happens when you remove `import Cocoa`? – NonCreature0714 Feb 09 '17 at 00:31
  • I deleted all the developer files and the application after doing what you said and it's working fine now, thanks for your help. –  Feb 09 '17 at 10:34
  • @Bowcox Well, I'm glad it was just a bad install, and nothing more! – NonCreature0714 Feb 09 '17 at 15:03