LoganHenderson
LoganHenderson

Reputation: 1452

Class extension inside a playground

I have a UIVIew subclass in my playground sources folder (its a Public class) and I'm trying to only put a portion of this class into my main playground file as an extension.

When I declare the following in my playground

public extension SortingAlgorithmView {

    public func typeTest() {
        print("dfasfndjasfasfdasfdasf test ")
    }
}

and then inside my subclass I call typeTest() it tells me there is no function with the name typeTest.

Upvotes: 4

Views: 2893

Answers (2)

DonMag
DonMag

Reputation: 77520

This works fine for me....

EDIT: OK, give this a try - again, works fine for me...

// extFuncs.swift --- in the sources folder

import UIKit

public extension SortingAlgorithmView {

    public func typeTest() {
        print("dfasfndjasfasfdasfdasf test ")
    }
}

public class SortingAlgorithmView: UIView {

}

// a page in the Playground

import UIKit
import PlaygroundSupport

let view = SortingAlgorithmView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
view.backgroundColor = UIColor.blue

view.typeTest()

PlaygroundPage.current.liveView = view

Upvotes: 4

NobodyNada
NobodyNada

Reputation: 7634

The interactive pages do not run alongside the source files; the source files are compiled first and then imported into your playground pages (which is why you need to make everything public). This makes them much faster since they can be compiled just once instead of interpreted every time, but it also means they cannot reference the code in the playground pages.

If you want to access the extension from the source files, you'll have to put it in a source file. The source files cannot access the interactive content.

Upvotes: 5

Related Questions