I have a custom class named Profile
and a NSMutableArray
which I add the profile objects in case I need to go back and forth in an iteration.
The code is this:
@try {
currentProfile = (Profile *) [cache objectAtIndex:(int)currentPosition-1];
}
@catch (NSException * e) {
Profile *cached = [[Profile alloc] init];
[cached loadProfile:currentPosition orUsingUsername:nil appSource:source];
cached.position = NULL;
[cache addObject:cached];
currentProfile = cached;
[cached release];
}
//And the "log" i use to show the error
Profile *temp;
for (int i=0; i<[cach开发者_StackOverflow社区e count]; i++) {
temp = (Profile *) [cache objectAtIndex:(int)currentPosition-1];
NSLog(@"%@ - %d e %d", temp.name, temp.position, temp.realId);
}
[temp release];
The NSLog is returning me lenght of cache times with the same object. I.E.
for len = 1: first - 1 e 1for len = 2:
second - 2 e 2 second - 2 e 2for len = 3:
third - 3 e 3 third - 3 e 3 third - 3 e 3and so on...
And what I need is: for len = 3: first - 1 e 1 second - 2 e 2 third - 3 e 3You probably want to use variable i
inside the loop, instead of currentPosition
for (int i=0; i<[cache count]; i++) {
temp = (Profile *) [cache objectAtIndex:i];
NSLog(@"%@ - %d e %d", temp.name, temp.position, temp.realId);
}
Otherwise, you're always retrieving the same object.
You may also want to consider 'for each' loop instead of a simple 'for'. Just for the sake of simplicity.
for (Profile *temp in cache) {
NSLog(@"%@ - %d e %d", temp.name, temp.position, temp.realId);
}
精彩评论