iosxcodeerror-handlingswiftuicgaffinetransform

Error message: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeead03e18)


I'm creating an app and I get this error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeead03e18). Whole code runs without any errors, it's just when I try to launch the app after several seconds I obtain this error.

This is a code of my app:

import SwiftUI

struct Flower: Shape {
    var petalOffset: Double = -20
    var petalWidth: Double = 100
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        for number in stride(from: 0, to: CGFloat.pi * 2, by: CGFloat.pi / 8) {
            let rotation = CGAffineTransform(rotationAngle: number)
            let position = rotation.concatenating(CGAffineTransform(translationX: rect.width / 2, y: rect.height / 2))
            
            let originalPetal = Path(ellipseIn: CGRect(x: CGFloat(petalOffset), y: 0, width: CGFloat(petalWidth), height: rect.width / 2))
            let rotatedPetal = originalPetal.applying(position)
            
            path.addPath(rotatedPetal)
        }
        
        return path
    }
}

struct ContentView: View {
    @State private var petalOffset = -20.0
    @State private var petalWidth = 100.0
    
    var body: some View {
        VStack {
            Flower(petalOffset: petalOffset, petalWidth: petalWidth)
                .stroke(Color.red, lineWidth: 1)
            
            Text("Offset")
            Slider(value: $petalOffset, in: -40 ... 40)
            padding([.horizontal, .bottom])
            
            Text("Width")
            Slider(value: $petalWidth, in: 0 ... 100)
                .padding(.horizontal)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Does anyone know how to solve this problem?


Solution

  • This was not an obvious problem. On the iPad it was causing a literal Stack Overflow. Through trial and error commenting out code I was able to find the culprit: a missing dot.

    Change:

    Slider(value: $petalOffset, in: -40 ... 40)
    padding([.horizontal, .bottom])
    

    To:

    Slider(value: $petalOffset, in: -40 ... 40)
        .padding([.horizontal, .bottom])