Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 984
Markus Rautopuro Avatar asked Jun 04 '14 18:06

Markus Rautopuro


People also ask

How do you create a playground File?

Create your Playground To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

How do I add a playground to my Xcode project?

Add PlaygroundIn the workspace, File > New > Playground > Single View. Save the playground file in the root folder, usually along with the Xcode xcworkspace and xcodeproj files. Drag and drop the playground file into the workspace. In the Playground page, import the framework, and set up a live view.


1 Answers

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

like image 151
Rick Ballard Avatar answered Oct 04 '22 17:10

Rick Ballard