Clear out contents in NSMutableData

3.6k Views Asked by At

I am new to Swift and iOS Development. How do we clear an NSMutableData without using release and then re-alloc/init again to be used again?

I have tried resetBytesInRange() but that replaces the contents by zero.

I tried data.length = 0, but there is garbage data at the end after appending.

data.setData(nil) gives an error.

I read this question but it doesn't solve my problem on swift.

Please help.

2

There are 2 best solutions below

0
On BEST ANSWER

Try using setData() with NSData():

mytableData.setData(NSData())
5
On

For Swift create new data object for current data will replace old bytes with empty bytes.

data = NSData(data: NSData())
println(data)

For mutable data:

mutableData = NSMutableData(data: NSMutableData())
println(mutableData)

Output: Empty bytes array

<>