4

When creating a new Swift Playground / .playgroundbook intended to be used on the iPad App, I often received the error message:

"Problem running playground. There was a problem encountered while running this playground. Check your code for mistakes."

I could track this issue down to be caused when adding certain subviews to my live view. To be more precise, my goal is to split a UIImage into multiple parts and create new UIImageViews for them:

for x in 0..<parts {
    for y in 0..<parts {
        //Create UIImageView with cropped image
        let pieceView = UIImageView.init(frame: CGRect.init(x: CGFloat(x)*singleSize.width, y:CGFloat(y)*singleSize.height, width: singleSize.width, height: singleSize.height))
        let imageRef = image.cgImage!.cropping(to: CGRect.init(x:0, y:0, width: 100, height: 100));
        pieceView.image = UIImage.init(cgImage: imageRef!)
        //Add them to an array
        self.viewArray.append(pieceView)
    }
}

And that's where things become very tricky for me: Adding 7 of these UIImageViews now works without a problem. But as soon as I want to add 8 or more of them, the playground stops working and gives the error message "Problem running playground..." (see above)

What I tested so far:

  • Adding UIImageViews with the same image does not cause this problem
  • Cropping the UIImage in a background thread and adding the view on the main thread does not help either
  • Creating the UIImageViews without adding them to the live-view does not cause any problems
  • The code works well when being executed on a mac playground, no matter how man views to add
jraufeisen
  • 3,005
  • 7
  • 27
  • 43

3 Answers3

9

I experienced this kind of iPad Swift Playground run-time error while adding multiple UI elements.

The problem caused by the default setting of "Enable Results" in the playground's property which is set to be ON. The "Enable Results" previews all the in-line object results' viewer. It makes the swift playground crashed when you produce many UI elements.

Try to disable the "Enable Results". It works for me. Swift Playground: Turn off for Enable Results

Community
  • 1
  • 1
Kiratijuta
  • 785
  • 7
  • 11
  • If you're attempting to write swiftUI code in playgrounds. "Do this one trick." to get swiftui running. I couldn't render a Tabview with more than 2 tabitems until I turned "Enable Results" off. I verified my code worked by recreating in xcode and by choosing "Step Slowly" Some of my views were being rendered x4 but it eventually worked after 5mins. Turn "Enable Results" off and the view rendered instantly – mushcraft Aug 03 '21 at 21:25
0

I saw a similar issue, and the problem was that it was just a nil incorrectly used.
In order to get a clear error message, just create a playground with the same files, and run it on the mac: in this way you can get more detailed information about what is going on and find easier to solve your issue.

Let me know if you find any difficulties :)

Diiaablo
  • 166
  • 1
  • 13
0

Probably is a late answer... but I was experiencing same issue during last week... finally today just figure it out how to solve:

I was running a piece of code where I add a background view as next:

func createView(){
    // gray background
    let marco = CGRect(x:0, y: 0, width: 603, height: 825)
    vista = UIView(frame: marco)
    vista.backgroundColor = UIColor.gray
    vista.isUserInteractionEnabled = true
    vista.tag = 5
    PlaygroundPage.current.liveView = vista
    PlaygroundPage.current.needsIndefiniteExecution = true
}

But I place this at the beginning of the code

What I have changed is :

PlaygroundPage.current.liveView = vista
PlaygroundPage.current.needsIndefiniteExecution = true

Placing it at the end of all the code, before you start to run your program... if you need more detail let me know and can share more information.

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55