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<[cache 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 1
for len = 2:
second – 2 e 2
second – 2 e 2
for len = 3:
third – 3 e 3
third – 3 e 3
third – 3 e 3
and so on…
And what I need is:
for len = 3:
first – 1 e 1
second – 2 e 2
third – 3 e 3
You probably want to use variable
iinside the loop, instead ofcurrentPositionOtherwise, 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.