Search code examples
iosswiftnsmutabledata

Clear out contents in NSMutableData


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.


Solution

  • Try using setData() with NSData():

    mytableData.setData(NSData())