Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

swift - What does "Thread 1: EXC_BAD_ACCES (code=2, address=0x7ffee73ceff8)" mean?

In the first view, the code takes an input (a latitude) and puts it into a url, which then is passed to another struct.

View 1:

class ViewModel: ObservableObject {
    @Published var latitude:String = ""
    @Published var page = 0
}

struct ContentView: View {
    @EnvironmentObject var value1: ViewModel
    
    var body: some View {
        if value1.page == 0{
            VStack{
                TextField("", text: $value1.latitude)
                Button(action:{ value1.page = 1}){
                    Text("To next view")
                }.frame(width: 300, height: 100, alignment: .center)
            }
        } else {
            elevationFunction(url1: URL(string: "https://midcdmz.nrel.gov/apps/spa.pl?syear=2020&smonth=1&sday=1&eyear=2020&emonth=1&eday=1&otype=0&step=60&stepunit=1&hr=12&min=0&sec=0&latitude=(value1.latitude)&longitude=10.757933&timezone=1.0&elev=53&press=835&temp=10&dut1=0.0&deltat=64.797&azmrot=180&slope=0&refract=0.5667&field=0"))
            elevationGraph()
        }
    }
}

In the second view, the html of the url is loaded, and some values are scraped of the website. These values are then used to create a graph (path). The Code runs perfectly until I press the button "Next View" then it crashes and shows the error "Thread 1: EXC_BAD_ACCES (code=2, address=0x7ffee73ceff8)"

View 2:

func loadData(from url: URL?) -> String {
    guard let url = url else {
        return "nil"
    }

    let html = try! String(contentsOf: url, encoding: String.Encoding.utf8)
    
    return html
    
}

struct elevationFunction: Shape {
    var url1: URL?
    let html = loadData(from: elevationFunction.init().url1) // The Error is shown here

    private func dbl1() -> Double {
        
        let leftSideOfTheValue = "0:00:00,"
        
        let rightSideOfTheValue = "(month)/(day)/(year),1:00:00,"
        
        guard let leftRange = html.range(of: leftSideOfTheValue) else {
            print("cant find left range")
            return 0
        }
        
        guard let rightRange = html.range(of: rightSideOfTheValue) else {
            print("cant find right range")
            return 0
        }
        
        let rangeOfTheValue = leftRange.upperBound..<rightRange.lowerBound
        
        return Double(html[rangeOfTheValue].dropLast()) ?? 90
    }

    
    
    func path(in rect: CGRect) -> Path {

        var path = Path()
        
        path.move(to: CGPoint(x: 10, y: (125 - (90-dbl1()))))
        path.addLine(to: CGPoint(x: 120, y: (125 - (90-45))))
        path.addLine(to: CGPoint(x: 250, y: (125 - (90-dbl1()))))
    
        var scale = (rect.height / 350) * (9/10)
        var xOffset = (rect.width / 6)
        var yOffset = (rect.height / 2)
        
        return path.applying(CGAffineTransform(scaleX: scale, y: scale)).applying(CGAffineTransform(translationX: xOffset, y: yOffset))
        
        
    }
}


struct elevationGraph: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                elevationLegend().stroke(lineWidth: 4.0).aspectRatio(contentMode: .fill)
                elevationFunction().stroke(lineWidth: 4.0).aspectRatio(contentMode: .fill)
            }
            .frame(width: 600, height: 800, alignment: .center)
        }
    }
}
question from:https://stackoverflow.com/questions/65882084/what-does-thread-1-exc-bad-acces-code-2-address-0x7ffee73ceff8-mean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...