1

I have the following code. When I iterate 25 times (5 * 5), it works fine. When I iterate 36 times (6 * 6 in the for loops), it crashes with SIGABRT. I've pasted an image of the error (Playgrounds in Xcode won't let me copy paste it). Is this an internal bug, is there a limit of 32 behaviours defined somewhere, or is this a bug in my code? Thank you! enter image description here

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    var animator : UIDynamicAnimator!

    var springfields : [UIFieldBehavior] = []

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        animator = UIDynamicAnimator(referenceView: view)

        for i in 1...6 {
            for j in 1...6 {
                let newView = UIView()
                let dotwidth = 12
                newView.layer.cornerRadius = CGFloat(dotwidth/2);
                newView.frame = CGRect(x:20+i*25, y:20+j*25, width:dotwidth, height:dotwidth)
                newView.clipsToBounds = true
                newView.backgroundColor = .black

                let newSpringField = UIFieldBehavior.springField()
                springfields.append(newSpringField)
                newSpringField.addItem(newView)
                newSpringField.position = newView.center

                view.addSubview(newView)

                animator.addBehavior(newSpringField)
            }
        }

        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
Tom H
  • 1,316
  • 14
  • 26
  • 1
    I'm assuming its an overflow error, but Apple hasn't made it easy to know that. I think as you are iterating between 25 and 36, I assume its when you do 32 iterations (can be checked using a print statement of which iteration you are on) it is failing. – George Jul 10 '18 at 17:11

1 Answers1

3

Running your code in an app, the Debug console output is:

*** Terminating app due to uncaught exception 'Invalid Association', reason: 'UIDynamicAnimator supports a maximum of 32 distinct fields'

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thank you! I didn’t realise that error messages in Playgrounds would be less informative than those in apps. Wonder why Apple put this limit in... – Tom H Jul 11 '18 at 21:34