-3

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.

OnkarK
  • 3,745
  • 2
  • 26
  • 33
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
  • 5
    NSMutableData *mutableData = [receivedData mutableCopy]; – h.kishan Jan 15 '14 at 05:34
  • 1
    I 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 Answers2

6
NSMutableData * data = [NSMutableData dataWithData:jsonData];

This should solve your problem.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Karim
  • 736
  • 3
  • 14
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