TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

[Pro SwiftUI] Lava lamp crashing when the timer is too frequent

Forums > SwiftUI

Hello, I'm learning SwiftUI by reading Pro SwiftUI book and coding the examples and I have some trouble with the more advanced version of lava lamp. My app crashes when the timer changing animatableData runs too frequently. To remind you, there is a polygon shape changing its shape every 1 second, but the animation takes 3 seconds. That way, as I was able to deduct from lots of print() statements, sometimes SwiftUI tries to compare/calculate animatableData before and after animation and that data have different dimensions, looks like one side is either .zero (of size 1) or 2 times larger than the other and I get index out of bounds error. What exactly causes SwiftUI to change the animatableData's dimensions that way? I thought those dimensions shall be constant.

    public static func +=(lhs: inout [Double], rhs: [Double]) {
        print("lhs size: \(lhs.count)\trhs size: \(rhs.count)")
        for (index, item) in rhs.enumerated() {
            lhs[index] += item
        }
    }

    public static func -=(lhs: inout [Double], rhs: [Double]) {
        print("lhs size: \(lhs.count)\trhs size: \(rhs.count)")
        for (index, item) in rhs.enumerated() {
            lhs[index] -= item
        }
    }
lhs size: 8 rhs size: 8
lhs size: 8 rhs size: 1
lhs size: 1 rhs size: 8
Swift/ContiguousArrayBuffer.swift:613: Fatal error: Index out of range

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.