Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode playgrounds can't access swift files in Sources folder

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?

like image 782
DerrickHo328 Avatar asked Apr 14 '15 21:04

DerrickHo328


People also ask

What is the difference between Swift playground and Xcode?

Xcode and Swift are both software development products developed by Apple. Swift is a programming language used to create apps for iOS, macOS, tvOS, and watchOS. Xcode is an Integrated Development Environment (IDE) that comes with a set of tools that helps you build Apple-related apps.

Why is my Xcode playground not running?

Restarting Xcode and rebooting my macbook. Stopping the simulator via the Activity monitor and restarting it. Opening up new tabs in an attempt to refresh. Uninstalling and reinstalling Xcode via the app store.


2 Answers

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

like image 150
Vitali Avatar answered Oct 24 '22 06:10

Vitali


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"   } } 
like image 34
nich Avatar answered Oct 24 '22 05:10

nich