user3862415 user3862415 -3 years ago 126
Objective-C Question

objective-c dot notation with square brackets

hi i don't understand this code of course CS193p

[[PlayingCard rankStrings][self.rank] stringByAppendingString:self.suit];

where rankString is variable method

+ (NSArray *)rankStrings
return @[@"?", @"A", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K"];

self.rank is a getter of random number

@property (nonatomic) NSUInteger rank;

and self.suit in another variable method

+ (NSArray *)validSuits
return @[@"♥️", @"♦️", @"♠️", @"♣️"];

I insert in my code NSLog to understand the functioning .... I understand that it takes rank from the rankStrings and concatenates them with the suit .... but I don't understand how! the method appendingString is clear ... but how do you get the values rank ​​from an rankStrings? the [PlayingCard rankStrings] is simple call to a method of variable and NSUInteger rank is a getter

Answer Source

[PlayingCard rankString] presumably, returns an array.

self.rank is providing an NSUInteger.

We can use the square-bracket notation to access an array index as such:

myArray[10] // this accesses the object at index 10 of myArray

[PlayingCard rankString][self.rank] is accessing the object at the self.rank index of the array returned by [PlayingCard rankString].

The object at that index is presumably a mutable string, so we're now calling a string method on the returned object.

The code snippet you provided could easily be rewritten as such:

NSArray *playingCardArray = [PlayingCard rankString];

NSMutableString *rankString = playingCardArray[self.rank];

[rankString stringByAppendingString:self.suit];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download