GodelEscher
GodelEscher

Reputation: 539

How to link pages in swift playgrounds?

I've got this class called User.

class User {
    var firstName: String
    var lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

    func fullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

In a swift playground page called "User Object" I've then got a playground page that is called "Main Program"

I would like to link the "User Object" page and the "Main Program" page together, so that I could something like this in the "Main Program" Page;

let User1 = User(firstName: "John", lastName: "Smith")

print(User1.fullName())

How do I link up these two pages in swift playgrounds

Thank you

Upvotes: 0

Views: 553

Answers (1)

TimTwoToes
TimTwoToes

Reputation: 711

You can't connect playground pages, but you can share your source file, using the Sources folder in the projects Sources folder.

If your playground project is named User, you can put the source file in the User Sources folder, and it is available to all the sub-pages in the playground.

Remember to make your class, variables and functions public. If you don't specify it, it will only be visible within the Page.

In this example I have made a project named User and created a page User Object. I placed the User.swift file into the User Object Sources directory. It is available to the User Object page.

Playground example

I uploaded the example to my Github account - https://github.com/timtwotoes/UserPlayground

Upvotes: 1

Related Questions