How to Import Own Classes from Your Own Project into a Playground

How to import own classes from your own project into a Playground

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

How to import my own class into an Xcode 7 playground?

In the Playground go to menu

View / Navigators / Show Project Navigator

and copy your class files into the Sources folder.

Your classes have to be marked public like this:

public class MyPlayGround {
public class func sayHello() {
print("Hello")
}
}

The Playground will then have access to the classes:

MyPlayGround.sayHello()

How do I include my app as a module in a Swift Playground?

Unfortunately, Playground comes with below limitations:

  1. Playground cannot be used for performance testing.
  2. Does not support User Interaction.
  3. Does not support On-device execution.
  4. Does not support custom entitlements.

Personally, I would want to see 2 in action at least :)!

So, to answer your question, its not possible (at least at this point in time).

How to I import 3rd party frameworks into Xcode Playground?

Edited: As of Beta5 this is now supported when the playground is part of the workspace that builds the framework as a target. There are more details on the Apple dev forums site, but hopefully @Rick Ballard will add them to his answer here and that should becoke the definitive answer for this question.

Don't do the below anymore!


For the short term, while there's no supported solution, if you're producing a Module/Framework you can copy it into the SDKs System/Library/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.

For an OS X Playground: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks

And for iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/

But before you do that... go file a radar as @Rick says in his answer.



Related Topics



Leave a reply



Submit