Reputation: 4301
I guess this is a really basic question but I am doing a tutorial from Nick Kuh's book "Foundation iPhone App Development" and i do not fully understand what this line:
int count = [self count];
...really initiates with the "self"?
Here is the whole code:
#import "NSMutableArray+Shuffle.h"
@implementation NSMutableArray (Shuffle)
- (void)shuffle {
int count = [self count];
NSMutableArray *dupeArr = [self mutableCopy];
count = [dupeArr count];
[self removeAllObjects];
for (int i = 0; i < count; i++) {
// Select a random element between i and the end of the array to swap with.
int nElement = count - i;
int n = (arc4random() % nElement);
[self addObject:dupeArr[n]];
[dupeArr removeObjectAtIndex:n];
}
}
@end
Upvotes: 1
Views: 4926
Reputation: 130222
Since you are in a category of NSMutableArray, self refers to an instance of NSMutableArray. Then count is a property of NSMutableArray that returns the number of objects contained by the array. So the line in question says get the number of items in the current instance of NSMutableArray and store that in a variable named "count" of type int.
int count = [self count];
This could also be written as the following, while remaining syntactically valid.
int count = self.count;
Upvotes: 5
Reputation: 2619
It's calling the 'count' method on itself. The syntax might be throwing you off, as is often the case when you first see Objective-C. In Java, it would look like this:
int count = this.count();
Upvotes: 3