liorco
liorco

Reputation: 1493

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

Upvotes: 0

Views: 602

Answers (2)

liorco
liorco

Reputation: 1493

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

Upvotes: 0

Cy-4AH
Cy-4AH

Reputation: 4585

The better is asynchronous response:

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

Upvotes: 1

Related Questions