objective-cpass-by-referencensdatansmutabledata

Objective C - What is better pass a ref to NSData or use NSMutableData


I am developing an API that returns an NSData to the caller. I need the object to be provided in the function parameters (not as a return value). Which of the approach below is preferred and why?

NSData* data;
[self foo1:&data];

-(BOOL)foo1:(NSData**)data {
  *data = [@"1234" dataUsingEncoding:NSUTF8StringEncoding];
  ...
}

or

NSMutableData* data = [[NSMutableData alloc] init];
[self foo2:data];

-(BOOL)foo2:(NSMutableData*)data {
  [data setData:[@"1234" dataUsingEncoding:NSUTF8StringEncoding]];
}

Solution

  • Thanks @Willeke. I am going to adopt your advice - Use NSMutableData* if the method adds data, use NSData** if the method creates the data.