Swift: Working around Sources playground bugs

The Xcode release notes are pretty clear on bugs that exists for a playground’s Sources folder:

Description: Sometimes Xcode will crash after editing a supporting source file for a playground and then viewing the playground itself. This happens when using the Project navigator to move back and forth between the playground and its enclosed files. (20094959)

Workaround: Open and edit the playground supporting source files in their own windows

Description: Playgrounds with supporting source files sometimes fail to execute, showing an error in the Console Output like: “Playground execution failed: error: Couldn’t lookup symbols: __TF16EnrichMe_Sources2hiFT_T_ __TF16EnrichMe_Sourcesau2piSd”. (20100043)

Workaround: View the supporting source file, then view and re-execute the playground

Description: Playgrounds with supporting source files sometimes fail to execute after changing the name of a function in the support file. You may see an error in the Console Output like: “Playground execution failed: MyPlayground.playground:6:1: error: use of unresolved identifier ‘myFunc’”. (20109247)

Workaround: Quit and relaunch Xcode. You may then need to view the supporting file, then view and re-execute the playground

There’s also a problem where new files are simply not recognized, and any material you add is ignored.  When this happens quit and relaunch Xcode. This keeps biting me when I develop code in the playground and then create a new Swift source file, paste the code in, and try to access it from the playground.

Hopefully this post will help you avoid the wasted time I spent.

 

6 Comments

  • I’ve run into the same problem as you several times; I was getting an error like `Could not import module MyPlayground_Sources`. I could only get it to work by resetting the Simulator and basically blowing away every non-essential Xcode file on my system:

    ~$ rm -rf ~/Library/Caches/com.apple.dt.Xcode.Playground
    ~$ rm -rf ~/Library/Caches/com.apple.dt.Xcode
    ~$ rm -rf ~/Library/Developer/Xcode/DerivedData
    ~$ defaults delete com.apple.dt.Xcode
    ~$ rm -rf /var/folders/k5/tctgqgkx3vs4q9xqmd30lbvh0000gn/* # !!!

    … and then creating a brand-new Playground, with the contents of the sources I wanted to add pasted one after the other in SupportCode.swift. Woof. Hope this gets sorted soon. I have used Playgrounds a lot so far! It will be so cool when they are, like, stable!

    • I’m trying onf of the code snippets in the Swift Developer’s Cookbook and can’t make it work. I’m with Recipe 2-1, declaring the class OutputStream in the playground file doesn’t show any errors but if I type the code above the recipe, I get either “Execution was interrupted, reason EXC_BAD_ACCESS (code=2…)” or if I create a file in the Sources folder, with the OutputStream class in it, i get “value of type ‘OutputStream’ has no member ‘path'”.

      Maybe I’m missing something here?. Any ideas?

      • I’m pretty sure that’s a known bug with playgrounds. Try running the sample code from github, which has it placed in a normal app and let me know if that’s better.

        (Quick update: just tried doing stuff in the latest playground and didn’t encounter errors — just did copy and paste from the github samples though)

  • I have come to the conclusion that playground are pointless. Too much work and not enough productivity. Maybe next year.

  • Ridiculous that after 6 months “Playground execution failed: error: Couldn’t lookup symbols:” is still a problem.

  • Its October and still hitting:

    Playground execution failed: error: Couldn’t lookup symbols:
    __swift_FORCE_LOAD_$_swiftCoreGraphics
    __swift_FORCE_LOAD_$_swiftDarwin
    __swift_FORCE_LOAD_$_swiftCoreImage
    __swift_FORCE_LOAD_$_swiftFoundation
    __swift_FORCE_LOAD_$_swiftDispatch
    __swift_FORCE_LOAD_$_swiftObjectiveC
    __swift_FORCE_LOAD_$_swiftUIKit
    _playground_logger_initialize
    _playground_log_postprint

    * thread #1: tid = 0x603d, 0x00000001060763c0 MyPlayground`executePlayground, queue = ‘com.apple.main-thread’, stop reason = breakpoint 1.2
    * frame #0: 0x00000001060763c0 MyPlayground`executePlayground
    frame #1: 0x00000001060759c0 MyPlayground`__37-[XCPAppDelegate enqueueRunLoopBlock]_block_invoke + 32
    frame #2: 0x0000000106b9025c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #3: 0x0000000106b75304 CoreFoundation`__CFRunLoopDoBlocks + 356
    frame #4: 0x0000000106b74a75 CoreFoundation`__CFRunLoopRun + 901
    frame #5: 0x0000000106b74494 CoreFoundation`CFRunLoopRunSpecific + 420
    frame #6: 0x000000010c00ca6f GraphicsServices`GSEventRunModal + 161
    frame #7: 0x000000010771ef34 UIKit`UIApplicationMain + 159
    frame #8: 0x00000001060756e9 MyPlayground`main + 201
    frame #9: 0x000000010a0cc68d libdyld.dylib`start + 1