What does "return @ [blah, blah] [self.foo]" mean?

I follow some tutorials, and there is a line of code that I really don't understand:

- (NSString *)rankAsString
{
return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"D",@"K"][self.rank];
}

What is coming back? Thanks!

+4
source share
1 answer

This is also called the lookup table and can be used instead of switch / case or if / else in such situations.

That is, the code creates an NSArray (from NSString) for use as a search, and then retrieves the string at the specified ordinal position - for example, Ace ("A") is rank 1, and king ("K") is rank 13.

Consider whether the code was written as:

NSArray* array = @[@"?", @"A", ..., @"K"];
return array[self.rank];

, . @[..], @".." array[..] Clang OBJECTIVE-C ; Apple LLVM Compiler 4.

+6

All Articles