Can Swift Playgrounds See Other Source Files in the Same Project

Can Swift playgrounds see other source files in the same project?

They cannot. Playgrounds are self-contained. This will hopefully change in the future.

Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.

Sources referencing each other in XCode Playgrounds

Add the public attribute to the classes, methods, and properties that you want to access in other playgrounds. This should solve your problem:

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

Update:

I found this thread, which although is not on this topic specifically I believe answers the question, here is what the accepted answer says:

They cannot. Playgrounds are self-contained. This will hopefully change in the future.

Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.

So, as I said before, thing is the sources folder can be accessed, but not thing in other playgrounds.

Xcode playgrounds can't access swift files in Sources folder

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

Is .playground a swift file? Who can 'see' it?

A .playground file is actually a bundle directory, which can be verified by right clicking it and selecting "Show Package Contents".

It includes a .swift file for the actual source code, which can be edited normally , as well as a .xcplayground file and a .xctimeline file, which are opaque and can't be edited through normal means.

You can also add files to it, such as images and other resources to use inside your playground.

It's not necessary to be present in application projects. It's used solely as a place to explore, without the need to create a project.

class in playground vs in program file

This is why I hate playgrounds. They are not really Swift. In real Swift, all executable code must be inside a function (e.g. a method of some class or struct or enum); you can't have lines like person.lastName = "Smith" just hanging in space like that.

So in a real iOS project you'd need to write something more like this:

class Human {
var firstName = ""
var lastName = ""
}

func test() {

let person = Human()

person.lastName = "Smith"
person.firstName = "Peter"

print (person.firstName)
print (person.lastName)

}

And even then nothing would happen until you actually call test(), and you can't do that except in a function. This is why people usually test code in the viewDidLoad of a view controller.

class Human {
var firstName = ""
var lastName = ""
}
class ViewController : UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let person = Human()

person.lastName = "Smith"
person.firstName = "Peter"

print (person.firstName)
print (person.lastName)

}
}

Can I put multiple playground files into one Xcode workbook?

You can right click on your top level file and select the "New Playground Page" button, which will give you a separate page.

new page

The below image shows the new page created after you click that button. "Untitled Page" is the initial page, and "Untitled Page 2" is the new page.

new page



Related Topics



Leave a reply



Submit