Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import my own Swift class into Playground

Tags:

I have seen several different posts on this subject, but none seem to solve what I think is a basic problem. In my project I have the following:

Hi Class I have a Hi Class with a since method shown below

func sayHi(){     println("hi")  } 

Playground I have a playground where I try to import my Hi Class.

The problem: My playground cannot see/import the Hi class. I know my Hi class is working as I can call it from a view controller without a problem.

Here the response from stack/apple forums that seem the most relevant

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, your playground must be in the same workspace as the project that produces your framework. 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). 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). Your "Build Location" preference (in advanced "Locations" settings) should not be set to "Legacy". If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes". Once all these conditions are fulfilled, importing your framework will work in a playground

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

Can anyone point me to a step by step on how to do this ?

like image 387
mday Avatar asked Oct 04 '14 12:10

mday


People also ask

Can Swift Playgrounds learn Swift?

But then what? Swift Playgrounds teaches concepts and uses real Swift structure, but it's not real code. It doesn't make an app, it just guides Byte around and solves puzzles. Swift doesn't have a real command called collectGem() after all.

How do I create a swift playground in Xcode?

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.


2 Answers

In Xcode 7 there is a Sources folder in the Navigator Cmd1 that will import any swift code locally in your playground.

playground

Beware that you need to mark the classes and functions in the Sources as public.

like image 176
Lachezar Avatar answered Oct 21 '22 22:10

Lachezar


It's been over 3 years and I really hope you have been able to figure this out. Anyway, here's an answer for anyone experiencing the same problem.

Source code within the Sources folder are automatically imported. Simply ensure your class and methods are marked as public. And your class has public init method.

See screenshot below.

enter image description here

like image 22
williamukoh Avatar answered Oct 22 '22 00:10

williamukoh