saurabh
saurabh

Reputation: 6775

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.

Upvotes: 4

Views: 3622

Answers (2)

Eimantas
Eimantas

Reputation: 49354

Try using setData() with NSData():

mytableData.setData(NSData())

Upvotes: 4

Mehul Patel
Mehul Patel

Reputation: 23053

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

<>

Upvotes: -1

Related Questions