Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
Maxwell Collard Avatar asked Jun 04 '14 04:06

Maxwell Collard


People also ask

Is Swift Playgrounds good for learning code?

Learning to code with Swift Playgrounds is incredibly engaging. The app comes with a complete set of Apple-designed lessons. Play your way through the basics in “Get Started with Code” using real code to guide a character through a 3D world. Then move on to more advanced concepts.

Can you make an app with Swift Playgrounds?

When you're ready to create your own app, you can start a new app project. In the Swift Playgrounds app on your iPad, go to the My Playgrounds screen.

What programming language does Swift Playgrounds use?

Learn serious code. Swift Playgrounds is a revolutionary app for iPad and Mac that makes it fun to learn and experiment with Swift — a powerful programming language created by Apple and used by the pros to build today's most popular apps.


1 Answers

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.

like image 142
Logan Avatar answered Nov 05 '22 09:11

Logan