JustinHK JustinHK - 1 year ago 63
iOS Question

Swift app crashing when setting property in main queue

I am using a property setters and didSet() values to set a "percent complete" property. All property access is done in the main queue, the property I am setting has no hooks or getters or setters, the object is fully initialized, and the fields I am using in the calculation are all there.

However, I am getting an infuriating EXC_BREAKPOINT error with no other details.

I suppose it's possible that this is a multithreaded access problem, but I see only empty stacks in all of the other threads.

Error Breakpoint:

Error Breakpoint

Debugger Values:

Both fields populated

Call Stack (these are all set or didSet):

enter image description here

Answer Source

You are getting an integer overflow because you are multiplying 22,667,272 by 99. Use Int64 rather than Int.

The largest value you can store in a 32 bit integer is 2,147,483,647. 22,667,272 * 99 is 2,244,059,928, so you get an integer overflow. The size of an Int will vary from architecture to architecture, so your code will work on a 64 bit device but not on a 32 bit. If you use Int64 explicitly you will avoid the overflow.

let totalBytes = Int64(self.bytesTotal)
let uploaded = Int64(self.bytesUploaded)

self.percentComplete = Int(1 + 99 * uploaded / totalBytes)