2

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

1 Answer 1

3

You 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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

^^ you answered faster, no need for 2 times the same post so deleting mine
@Jason Check out stackapps, they have some useful notification utilities :)
God, thank you!! That was fast and very useful. And I feel stupid =(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.