skip to Main Content

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?

2

Answers


  1. 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])
    
    Login or Signup to reply.
  2. Had the same problem and bashed my had a good hour. In may case I missed a dot before onChange.

    It’s so strange to still see this problem in SwiftUI, no warning to error details to lead to the problematic line…

    For those who are facing same EXC_BAD_ACCESS (code=2, address=0x000000) double check your view modifiers, good luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search