I have the following code in my app, which iterates over an array of objects to find the max value of one of the fields:
var high: Float = 0
let readings: [Reading] = test.readings
for i in 0 ... readings.count {
let load = readings[i].loadReading
if load != nil{
if load > high{
high = load!
}
}
}
This code works fine in debug mode, but invariably crashes in release mode. The values have all been used previously in the loop, and produce the correct effects. test.readings is optional, but the code will not progress to this loop if it is. Has anyone got any ideas about what may change between debug and release mode that will cause this?
Lots of compiler optimizations are enabled in release but not in debug mode because they require extra compile time, which gets pesky during debugging. And it turns out that a lot of compiler optimizations involve loops. I suspect that one of those optimizations is not working correctly. To verify this:
•Go to your app's build settings.
•Search for the "Optimization Level" build setting.
•Expand it if necessary and switch the release setting to None [-O0]
•Maybe also disable the optimization profile usage (search "Use Optimization Profile")
•Now build and run again in Release.
Hopefully the loop will work now. If it does, the problem must be in the optimizations, so file a bug at bugreport.apple.com.