24

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 ?

Community
  • 1
  • 1
mday
  • 427
  • 1
  • 7
  • 22
  • related answer over here: http://stackoverflow.com/questions/24046160/how-to-i-import-3rd-party-frameworks-into-xcode-playground – Steve Rosenberg Oct 04 '14 at 13:58
  • 2
    http://stackoverflow.com/questions/24045245/how-to-import-own-classes-from-your-own-project-into-a-playground – nroose Nov 09 '14 at 22:57
  • 2
    I was after this question some time ago too, found answer(s), made it work, and came to the conclusion that the benefit is hardly worth the effort. There are too much of conditions to conform to in order to make it work and playground works shaky still. If I need to test how my app code works I use test-framework, if I want to test some idea or concept I use playground. – 0x416e746f6e Mar 17 '15 at 17:23
  • 3
    You may also want to have a look at the updates published on the Apple Swift Blog just a few days ago. https://developer.apple.com/swift/blog/?id=26 – Kilian Mar 22 '15 at 22:55

2 Answers2

23

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.

Aamir
  • 16,329
  • 10
  • 59
  • 65
Lachezar
  • 6,523
  • 3
  • 33
  • 34
  • 4
    **Mark classes and functions as public** Perfect – arsenius Jan 04 '17 at 00:49
  • Even if i mark my function public as showed in the image when i return in the 'MyPlayground' file, i could not use my function. Should i import the swift file in my playground? – Coder May 26 '20 at 11:02
  • Do you have any Swift Compiler Errors in the file where your function is in? I'm saying this because I just had the problem mentioned and all my functions were marked as public. After I fixed the errors in the file I could import with no issues. – Henrique Gouveia Jul 25 '21 at 23:55
  • Also, I just noticed that a "fileprivate" statement in one of my variables was preventing the rest of the file to be recognised. – Henrique Gouveia Jul 26 '21 at 00:59
9

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

williamukoh
  • 566
  • 6
  • 9
  • What if I want to be able to import that same module into multiple playgrounds/projects without copying and pasting it into each one's sources folder? Is there some global location where I can store all of my files? – Peter Schorn Mar 18 '20 at 20:48
  • @PeterSchorn were you able to figure out an answer to your question? I'm trying to do the same – apunn Jul 16 '20 at 21:29
  • I have discovered that playgrounds do not support remote packages. A playground is an entirely self-contained folder. Your best option is to create a macOS command-line project instead. – Peter Schorn Jul 17 '20 at 16:47
  • @apunn Although, I did create a small app that will clone a remote repository and move it to the sources folder of a playground. If the repository itself has other dependencies you will have to manually add those as well. – Peter Schorn Jul 18 '20 at 05:01