47

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Maxwell Collard
  • 2,587
  • 3
  • 16
  • 12
  • I asked the same question here: http://stackoverflow.com/questions/24016561/is-playground-a-swift-file-who-can-see-it – bauerMusic Jun 04 '14 at 16:28
  • 1
    bauerMusic: your question is different, this is about letting a playground access Swift files (classes) somewhere outside the playground. – Markus Rautopuro Jun 04 '14 at 16:54
  • Excellent question. Pity the answer wasn't what I wanted to hear, still, the truth is the truth. – Jonathan Jun 19 '14 at 18:49
  • 3
    possible duplicate of [How to import own classes from your own project into a Playground](http://stackoverflow.com/questions/24045245/how-to-import-own-classes-from-your-own-project-into-a-playground) – mmmmmm Jul 28 '14 at 20:44

4 Answers4

35

There's two ways to use your project's code in a Playground

Playground's Sources Folder

Yes, in Xcode 6.3 Beta 3 (and hopefully, into the future):

Playgrounds are now represented within Xcode as a bundle with a disclosure triangle that reveals Resources and Sources folders when clicked. These folders contain additional content that is easily accessible from your playground’s main Swift code. To see these folders, choose View > Navigators > Show Project Navigator (or just hit Command-1).

Open up a new playground and hit cmd + 1 to see the left pane, then drag files into the source folder to use within the playground.

Note:

The files in the source folder are compiled to a framework which means if you want classes, functions, etc. to be accessible in the playground, they must be explicitly marked as public.

public class VisibleClass {
}

class InvisibleClass {
}

Source: release blog

Compile Project Into Framework

  1. Move project to workspace if it isn't already. (File -> Save as Workspace) will do the trick
  2. Add framework target to your project
  3. Build framework
  4. Make sure files you want to access are added to your framework target
  5. Add Playground to workspace (NOT the project)
  6. Use @testable import YourFrameworkName
  7. Access code in playground

I made a write up here that goes into a bit more detail if you want to check it out.

Logan
  • 52,262
  • 20
  • 99
  • 128
  • 2
    Didn't work for me unfortunately. I still get a "No such module 'MyClass'" error. The class is specifically public and copied to the Sources directory. – Jon M Apr 10 '15 at 18:44
  • @JonM - There must be something else going on in your project, I can't really debug from your current description. It's also a pretty new feature, so it's bound to have hickups. Keep trying :) – Logan Apr 12 '15 at 14:40
  • Not a problem @Logan - thanks for the response. I've managed to work around it by not relying on pure playground to test :) – Jon M Apr 12 '15 at 15:29
31

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.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • I managed to create a workaround for this (http://stackoverflow.com/questions/24045245/how-to-import-own-classes-from-your-own-project-into-a-playground/24088624#24088624) but Xcode seems to crash more often when using Swift files like that. – Markus Rautopuro Jun 25 '14 at 16:02
  • This has officially changed w/ the release of Xcode 6.3 (see below: http://stackoverflow.com/a/29220769/2611971) – Logan Apr 12 '15 at 14:38
  • 1
    @Logan: While it's true that Xcode 6.3 now allows for supporting swift files in a Playground, it's still true that adding a Playground to a project does not allow the Playground to see other code in the project. – Lily Ballard Apr 13 '15 at 16:47
  • What? Then how are we supposed to test application files individually? Are you saying that we have no control over the linking even for tests and have to run an entire application just to test a simple model structure? Please see: http://stackoverflow.com/questions/28128169/how-can-i-test-swift-files-in-xcode-using-xctest-without-having-to-build-the-ent – Timothy Swan Aug 17 '15 at 22:55
  • Just a comment. In order to access the code in the supporting folder, it looks like it has to be set to 'public' and not 'internal' or else the main playground can't see it. – Mark A. Donohoe Sep 16 '18 at 23:19
5

Yes. I started by just adding a class file in the Sources directory. I made everything public:

  • class
  • init
  • members

After much trying, nothing worked. The XCode crashed and after reopening it all worked like a charm.

Using Sources/Dna.swift in Playground

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Uki D. Lucas
  • 516
  • 6
  • 4
0

In Xcode 10's Project Navigator:

  1. Add the source code file to the playground's Sources folder.
  2. Drag the file from the playground's Sources folder to the desired location in the project (you should see the little "plus in a circle" icon appear.
  3. End the drag and then in the Add File dialog uncheck "copy if needed"

The source file now "lives" in the playground package; the Project refers to it (you can verify that with the File Inspector).

I tried it the other way around: file lives in project folder with reference in playground's Sources folder but it didn't work; I ended up with two copies of the source code file.

Verticon
  • 2,419
  • 16
  • 34