EXC_BAD_INSTRUCTION (code=EXC_I386_INV - Why are we getting this error at } else if temperature > temperatureArray[dayNumber] {

EXC_BAD_INSTRUCTION (code=EXC_I386_INV - Why are we getting this error at } else if temperature > temperatureArray[dayNumber] {
0.0 0

#1
let dataTask = session.dataTask(with: weatherURL) {
        
        (data: Data?, response: URLResponse?, error: Error?) in
        if let error = error {
            print("Error:\n\(error)")
        } else {
            if let data = data {
                let dataString = String(data: data, encoding: String.Encoding.utf8)
                print("All the weather data:\n\(dataString!)")
                var temperatureArray: Array<Float> = Array()
                var dayNumber = 0
                var readingNumber = 0
                if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
                    if let mainArray = jsonObj!.value(forKey: "list") as? NSArray {
                        for dict in mainArray {
                            if let mainDictionary = (dict as! NSDictionary).value(forKey: "main") as? NSDictionary {
                                if let temperature = mainDictionary.value(forKey: "temp_max") as? float_t {
                                    if readingNumber == 0 {
                                        temperatureArray.append(temperature)
                                    } else if temperature > temperatureArray[dayNumber] {
                                        temperatureArray[dayNumber] = temperature
                                    }
                                    
                                } else {
                                    print("Error: unable to find temperture in dictionary")
                                }
                            } else {
                                print("Error: unable to find main dictionary")
                            }
                            readingNumber += 1
                            if readingNumber == 8 {
                                readingNumber = 0
                                dayNumber += 1
                            }
                        }
                        
                        
                        DispatchQueue.main.async {
                            
                        
                            self.weatherLabel1.text = "Day 1: \(temperatureArray[0])°C"
                            self.weatherLabel2.text = "Day 2: \(temperatureArray[1])°C"
                            self.weatherLabel3.text = "Day 3: \(temperatureArray[2])°C"
                            self.weatherLabel4.text = "Day 4: \(temperatureArray[3])°C"
                            self.weatherLabel5.text = "Day 5: \(temperatureArray[4])°C"
                            
                        }
                        
                    }
                } else {
                    print("Error: unable to convert json data")
                }
            } else {
                print("Error: did not receive data")
            }
        }
    }
    dataTask.resume()
}

}

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

What language is this?


#4

Hi Kevin thanks for the help, this is swift, I upgraded to xcode 9.3 and this started happening. It worked great before.


#5

Sorry, don’t know anything about Swift. Maybe someone else can help. We’re mostly focused on web dev here, but I’m sure someone has some idea.