is there some way to convert an NSData
object, into an NSMutableData one? Basically I am using NSJSONSerialization
which basically takes some JSON data I receive and transforms it into an NSData
object instance. Is there anyway to then transform this into an NSMutableData
object instance? I need to do this because later on, I will be spawning some threads and within the code blocks I provide, I would like to change the value of the Data object. I can't do that if its NSData
but I can if its NSMutableData
.
Asked
Active
Viewed 8,885 times
-3
-
5NSMutableData *mutableData = [receivedData mutableCopy]; – h.kishan Jan 15 '14 at 05:34
-
1I have no idea how you intend to hang the NSData. Normally you would serialize the received data to dictionaries or arrays and work with them. – vikingosegundo Jan 15 '14 at 05:48
-
@vikingosegundo this is a good point. Here's a line of code i use in my project. NSMutableDictionary * leaderBoardJsonData = (NSMutableDictionary)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; – Karim Jan 15 '14 at 05:56
2 Answers
6
NSMutableData * data = [NSMutableData dataWithData:jsonData];
This should solve your problem.

vikingosegundo
- 52,040
- 14
- 137
- 178

Karim
- 736
- 3
- 14
-
-
If I call the dataWithData command on the same NSMutableData object instance multiple times, will it then re-write the data each time since its mutable? – AyBayBay Jan 15 '14 at 05:41
-
2you don't call `dataWithData:` on an object. you call it on the class. – vikingosegundo Jan 15 '14 at 05:43
-
@vikingosegundo said it, call it on the actually NSMutableData class, not the instance of the class (data) – Karim Jan 15 '14 at 05:50
1
You can use below code:
NSData *data = ...;
NSMutableData *mutableData = [[NSMutableData alloc] initWithData:data];

vikingosegundo
- 52,040
- 14
- 137
- 178

Ashutosh
- 2,215
- 14
- 27