0

I am creating an app where I need to modify specific bytes in a Data object, but it crashes when I modify too many bytes at one time.

I thought the problem might be because my app was hanging for too long, but I put my code in a DispatchGroup and it didn’t help.

//amount and interval are Ints
var pos: Int = 1
let count = data.count
var tempData: Data = data
while (pos < count) {

    tempData[pos - 1] = tempData[pos - 1] + UInt8(amount)

    pos += interval
}

This code crashes my app when I provide it with a large Data object, but works fine with small ones.

Bytopo
  • 13
  • 3

1 Answers1

0

I found my problem. Since I was adding two UInt8s together, there was a chance that the resulting UInt8 would be invalid (greater than 255), which resulted in a crash. I fixed this by changing my + to &+ so the UInt8 overflows back to 1.

Bytopo
  • 13
  • 3