How does NSMutableData work?

13,040

Check if receivedData == nil. If so, then you might have forgotten to initialize it. For example:

receivedData = [[NSMutableData alloc] init];

Then release it when you don't need it anymore:

[receivedData release];
receivedData = nil;
Share:
13,040
BlueDolphin
Author by

BlueDolphin

Coding is my favorate game, it's fun and addicting.

Updated on June 15, 2022

Comments

  • BlueDolphin
    BlueDolphin almost 2 years

    I have one problem to work with NSMutableData.

    I defined one NSMutableData *receivedData, and tried to copy several NSData* data to the receivedData. I just called [receivedData appendData:data], but appears the data is not copied:

    ....
    NSLog(@"get data! Received %d bytes of data",[data length]);
      // output is not zero, say 1231.
    
    [receivedData appendData:data];
    NSLog(@"after append! length is %d bytes of data",[receivedData length]);
      // showing zero
    

    Thanks.

  • BlueDolphin
    BlueDolphin over 15 years
    Yes, that's the problem. Originally I thought if it is nil, it will report error. Thanks so much.
  • Abizern
    Abizern over 15 years
    In Objective-C it is legal to send messages to nil, so no error there.