Search code examples
swiftxcodedirectoryswift-playground

Can Swift playgrounds see other source files in the same project?


I created the most simple custom class in a separate Swift file in my project:

class Foo
{
    init()
    {
        println("I made a foo.")
    }
}

Then, in a playground within the same project, I tried

var x = Foo()

Xcode didn't seem to like this, and told me that 'Foo' is an unresolved identifier. I'm somewhat confused about how playgrounds fit into the rest of the project structure, since any other Swift file in my project can resolve 'Foo' without issue.

How can I make my playground able to use custom classes I define in other Swift files in my project? I have tried naming the product module for the build target and importing that into the playground, with no success: the playground doesn't recognize the name of the product module.

Thanks in advance for the assistance. I know it is something simple.


Solution

  • They cannot. Playgrounds are self-contained. This will hopefully change in the future.

    Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.