102

I just upgraded to Xcode 6.3 and they offered something new to the Playgrounds. If you make a new playgrounds and you open the project navigator, you will see a Sources folder and inside that there is a "SupportCode.swift" file. At the top of that file it reads

This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to .playground.

I tried putting a function in to there and it is not available to my playground. What am I doing wrong? Do I have to manually compile the SupportCode.swift file manually? How?

stkent
  • 19,772
  • 14
  • 85
  • 111
DerrickHo328
  • 4,664
  • 7
  • 29
  • 50

3 Answers3

255

You have to add public access attribute to your classes, methods and properties in source folder to make them accessible from main playground file as they treated as separate module by compiler

Vitali
  • 2,576
  • 1
  • 11
  • 3
  • 30
    I find it really odd that I have to make a class public AND make its init function public. – DerrickHo328 Apr 18 '15 at 02:16
  • 16
    would be great if we got something like the `@testable import` for playgrounds – dreamlab Jun 16 '15 at 22:24
  • 5
    @Calimari328 Declaring you class as public makes it visible outside, but making init() as public it's to instantiate (create instances) from outside. So when you want to give an access to the class but at the same time limit it's instantiation - you declare init() as less visible. I gave an example with Singleton implementation - to make it clearer – Nikita Kurtin Oct 27 '15 at 13:01
23

Playgrounds are good for running tests. Put all your code in the Sources directory and have one publicly accessible 'test' class, for each test. Then run the publicly accessible tests from the playground.

playground

Test1.run()
Testx.run()
...

Sources/Test1.swift

public class Test1 {      
  public static func run() {
    let my_class = MyClass()
    let result = my_class.do_something()
    print(result)
  }
}

Sources/MyClass.swift

class MyClass {
  func do_something() -> String {
    return "lol"
  }
}
nich
  • 239
  • 2
  • 4
  • Why do you need the Test1 class and the static 'run' method? Why not just create a public global test1() function? Achieves the same thing as the class doesn't add anything. – Mark A. Donohoe May 14 '18 at 19:39
15

As mentioned when you create .swift files in Source folder they are automatically available to your playground code. To control the access of different parts of this file you can use Access level modifiers which are: public, internal & private.

According to Swift programming language access control

The default access level in most cases is internal which accessible inside the module, but not the outside.

In other words, if you declare a class without access modifier you can access it from another file in Source folder but NOT in your playground's main file. in other hand if you declare a class with public modifier you can access it in both cases.

for practical usage: let's make an Singleton implementation First: I create a new file in Source folder named 'Singy.swift' with following code:

public class Singy {
    public var name = ""
    private static var instance: Singy?
    private init() {}

    public static func getSingy() -> Singy {
        if Singy.instance == nil {
            Singy.instance = Singy()
        }
        return Singy.instance!
    }
}

Second: from my playground

var s1 = Singy.getSingy()
var s2 = Singy.getSingy()
s1.name = "One"
print(s2.name)

Both s1 and s2 reference the same instance, but it created only within the class

Benj
  • 736
  • 7
  • 21
Nikita Kurtin
  • 5,889
  • 4
  • 44
  • 48