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.
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.
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.
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.
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
@testable import YourFrameworkName
I made a write up here that goes into a bit more detail if you want to check it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With