Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C equivalent of 'tuple unpacking'

Tags:

objective-c

Sometimes, I get sad when I can't use Python. In Python, I handle an array of arguments, unpacking them as such:

name, handle, parent_handle, [left, top, right, bottom], showing, scrollable = data

I must now do the same in Objective-C, with NSArrays. Am I doomed to 11 lines of:

NSString *name = (NSString *)[data objectAtIndex:0];
NSNumber *handle = (NSNumber *)[data objectAtIndex:1];
//....

or is there a better way?

like image 605
Claudiu Avatar asked Sep 10 '13 18:09

Claudiu


1 Answers

Yes. You are doomed. DOOMED! Mwah ha ha ha ha!

You can omit the casts and use subscripting to make it a little bit shorter though:

NSString *name = data[0];
NSNumber *handle = data[1];
// ...

You can omit the casts because both objectAtIndex: and subscripting return type id, which can be converted to any Objective-C class type without casting.

like image 85
rob mayoff Avatar answered Oct 10 '22 22:10

rob mayoff