I want to return multiple values from a method and I’ve decided to use an array to do so
-(NSArray *) foo {
// some operations here
return @[node, [NSNumber numberWithInt:i], [NSNumber numberWithBool:flag]];
}
an example is
-(NSArray *) foo {
return @[@"hi", [NSNumber numberWithInt:3], [NSNumber numberWithBool:YES]];
}
Is this a preferred way, and since there is an NSArray object created like that, which needs to stay, but can be released when there are no new owners later, is it true that this needs to be in an autorelease pool?
-(NSArray *) foo {
@autorelease {
// some operations here
return @[node, [NSNumber numberWithInt:i], [NSNumber numberWithBool:flag]];
}
}
That autoreleasepool is unnecessary. The runloop already has an autorelease pool, and the array you are creating on the return is tagged as autorelease, so, this array will be released.
You do not return multiple values from an objective-c method, just like you do not return multiple values from c. You can pass references to the method and assign the values within the method, or return collection objects containing the data you wish to return, or create a custom class that contains the response you wish to return from the method.