Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limited number of views in Swift Playgrounds

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
like image 837
JoRa Avatar asked Feb 05 '23 22:02

JoRa


1 Answers

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

like image 79
Kiratijuta Avatar answered Feb 07 '23 11:02

Kiratijuta