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

580 Views Asked by At

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]];
}
2

There are 2 best solutions below

0
liorco On BEST ANSWER

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

1
Cy-4AH On

The better is asynchronous response:

- (void)fooWithCompletion:(void (^)(NSData *responseData, NSError *responseError))completion;