Pseudo support for multiple return values in Objective-C

Originator:bigzaphod
Number:rdar://11825488 Date Originated:July 6, 2012
Status:Duplicate/4679208 Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
 
Every now and then I find I sort of wish Objective-C had a Perl-like (and probably other languages) ability to return multiple values from an array into individually named variables without needing to deal with the array explicitly all the time.

Example:

(NSString *title, NSString *otherThing, NSNumber *somethingElse) = [info getArrayOfThings];

In this case, -getArrayOfThings would return a normal NSArray - nothing special there and no need for any annotations or anything. All this would do is, when compiling, it would generate code like the following:

NSArray *_tempArray = [info getArrayOfThings];
NSString *title = [_tempArray objectAtIndex:0];
NSString *otherThing = [_tempArray objectAtIndex:1];
NSNumber *somethingElse = [_tempArray objectAtIndex:2];

If the array is not big enough, it'd fail at runtime in the same way as if I had written the code explicitly and tried to get something at an index that didn't exist in the array.

Seems like it'd be handy on occasion, anyway, but I admit it's current nonexistence is not exactly a glaring deficiency in the language or anything like that.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!