How to import own classes from your own project into a Playground
Asked Answered
M

6

79

Assume a setup like this:

  • You have an Xcode 6 project, where you've implemented your own classes (say MyView and MyViewController) with both Objective-C and Swift
  • You have added a Playground into your project

In the Playground, it's possible to import modules (frameworks) like UIKit with the import keyword. How do you enable access to the project's other classes from the Playground?

Just trying to access project classes directly results with an error message: Use of unresolved identifier 'MyView'

Manichaeism answered 4/6, 2014 at 18:46 Comment(3)
Wish there was an up to date answer for this question (7.3+)Hagen
please check: #28302162Eulau
please check this question and answer: #28302162Eulau
D
57

As of Xcode 6.0 Beta 5, it is now possible to import your own frameworks into a playground. This provides a way to share code between your applications and playgrounds, which can both import your frameworks. To do this:

  1. Your playground must be in the same workspace as the project that produces your framework. Your workspace must contain a target that produces the framework, instead of using a pre-built framework.

  2. You must have already built your framework. If it is an iOS framework, it must be built for a 64-bit run destination (e.g. iPhone 5s), and must be built for the Simulator.

  3. You must have an active scheme which builds at least one target (that target's build location will be used in the framework search path for the playground).

  4. Your "Build Location" preference (in advanced "Locations" settings of Xcode) should not be set to "Legacy".

  5. If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes".

  6. You must add an import statement to your playground for the framework.

Once all these conditions are fulfilled, importing your framework will work in a playground.

In Xcode 7 we introduced another mechanism that you can use to import your own classes as source, instead of importing a framework; you can read about this "Auxiliary Sources" support at http://help.apple.com/xcode/mac/8.0/#/devfa5bea3af

Dianoia answered 4/6, 2014 at 23:2 Comment(11)
"Playgrounds support importing frameworks." XCode 6 beta 5 Change-logTuggle
What about in a 'standalone' playground that exists independently of an Xcode project?Invulnerable
To complicated. Apple should make it easier.Upstart
So, it's not possible to import classes from an app target right ?Bonnice
This doesn't seem to work in Xcode 6.1. I've followed Ballard's requirements above which are echoed here as a tutorial, and the import statement in the playground raises an error.Invulnerable
After set "Build Location" to "Legacy". Can't build my project - got linker error: ld: file not found: /.../build/Debug-iphonesimulator/...Snitch
@RicardoPedroni "Sorry, that page cannot be found."Snitch
Just a note: You must build Simulator binary. Building Device binary has no effect. (Xcode Version 6.3.2 (6D2105))Shiller
@Snitch Silly Apple and their ever moving urls :/ (updated here)Brandenbrandenburg
Just for clarification, when you say that the playground must be in the same workspace as the project the produces the framework, you mean simply adding the SomeFramework.framework to the workspace will not work on its own?Tartaglia
That's correct; there must be a target in the workspace that produces the framework.Dianoia
M
18

I actually managed to refer to other Swift files in the current project by doing this:

  • Create an empty Playground and place is somewhere under your project.
  • Open the YourPlayground.playground bundle (yes, it's a bundle = directory) in Terminal.
  • Edit contents.xcplayground for example with vi and add another section like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'> 
   <sections>
       <code source-file-name='section-1.swift'/>
       <code source-file-name='section-2.swift'/>
   </sections>
   <timeline fileName='timeline.xctimeline'/>
</playground>
  • Rename section-1.swift to section-2.swift (if you created the Playground from scratch, there should be an example section-1.swift in your bundle)
  • Add a hard link (symbolic link doesn't seem to work) named section-1.swift which will point outside the bundle to your Swift class file like:
ln ../../Classes/MyView.swift section-1.swift
  • Close Xcode and open the Playground again.
  • Now there should be two sections, one with the contents of your Swift class file and the other one with the example content you got from creating the Playground from scratch.

This way I can actually run code lying outside the Playground, but Xcode seems to crash more often when doing it like this.

Edit:

As of Xcode 6 beta 5 you're now able to refer to project files, as Rick Ballard instructs in his answer.

Manichaeism answered 6/6, 2014 at 18:41 Comment(1)
Note that this will result in the same file appearing and being editable in two places. If you add style='setup' to the <code> tag it will appear as a collapsed/expandable block, which might be more ideal, as it hides the extra file to some extent.Embonpoint
P
13

Since Beta 5 of Xcode 6 it is possible to import your code if it is in a framework. What you need to do is create a framework target, add the Swift files there and in your playground do

import ModuleName

You can look up the module name in the build settings. It's usually the same as the target name.

Remember to make the code you want to see public. You'll need to build the project before changes are available in the playground. (You'll also need to edit the playground to trigger re-execution.)

Important

Do not give the playground file the same name as the target! If you do, importing seems to work but you'll get the following error when the playground tries to execute:

Playground execution failed: error: Couldn't lookup symbols:

I wasted an hour on figuring that out. :)

Philipphilipa answered 6/8, 2014 at 8:31 Comment(1)
Tip: No need to declare symbols as public. Just mark your import as @testable import ModuleNameSiphonophore
L
10

I wasn't able to get it working using any of the answers here, so I started playing around and found a simple way that worked for me to import a swift class into a playground.

Just create a playground in your project, theres a directory inside it called 'sources', just drag a copy of the swift class into that folder and the playground then will have access to it.

For example:

enter image description here

Lauricelaurie answered 14/10, 2015 at 2:8 Comment(1)
This worked for me. To other noobs, remember to also ensure you declare stuff as public that you want to use in the playground. developer.apple.com/library/ios/documentation/Swift/Conceptual/… - I have yet to find a Swift resource that bridges the gap between learning Swift (easy) and actually organizing a Swift project in XCode (so far very unintuitive).Lorolla
I
5

I just put links to all my swift files in the Sources folder:

cd /path/to/project/MyPlayground.playground/Sources
ln -s ../../*.swift .

This way changes in your source file will take effect in your playground immediately. Worked very nicely.

Xcode 8.2, Swift 3.0.1, macOS Sierra

Ibnrushd answered 15/12, 2016 at 21:36 Comment(2)
returns 'No such file of directory'. Can you help pleaseSkive
@ZaEeMZaFaR just mkdir those directories inside your .playgroundSmite
S
-1

All you have to do - is write in the beginning:

import ModuleName

(assuming your playground placed in the same workspace as framework/project)

If it's doesn't work:

  1. Rebuild your project

  2. Recreate playground and copy all from old playground there

It solves a lot of strange errors with failed init's and imports of whatever!

Snitch answered 5/6, 2015 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.