Search code examples
iosswiftprotocolsscenekitswift-playground

Swift Playground Protocol Crashing after Fixed Number of Iterations


For this year's WWDC scholarship, the format required is a Swift playground. I'm building my playground in an app, where I have sliders in UITableViewCells connected to a SCNScene and SCNNode and SCNParticleSystem via a custom delegate. It works perfectly fine running as an app, running on the Mac, but when it comes to the running it in swift playgrounds on the iPad, the delegate method crashes after a constant number of iterations. I have determined it is not the method in the node itself, as I made this print("Hello") and it still crashed with the exact same 96 iterations. The node has been in both Source files and the main playground file. Any ideas or help would be greatly appreciated.

Delegate

protocol ReturnParameterDelegate {
    func parameter1(value: Float)
    func parameter2(value: Float)
    func defaultParameter(value: Float)
}

Table View Cell

@objc private func returnValue() {
    guard let delegate = delegate else { return }
    print(self.parameter.title)
    switch self.parameter.title {
    case ParameterNames.parameter1:
        delegate.parameter1(value: self.slider.value)
    ...
    }
}

Scene

public func parameter1(value: Float) {
    node.changeValue(value: value)
}
...

Solution

  • After many hours of work and trying this with other methods, I have found a simple solution: move code from the main .playground to separate .swift files in the Sources folder. While this shouldn't change anything, adding it to the sources made all of my methods work perfectly. This does complicate protocols and I replaced them with creating a global object instead, however. It does remove the crash after a constant number of iterations and it makes certain other functions work that were not previously (for example, changing the diffuse of a SCNNode).