views:

46

answers:

1

I'm experiencing a weird problem. I have a for loop, inside the loop there is a switch statement. The counter in the for loop is a pointer that points to the beginning of a primitive array, it increments itself till it finds a "terminator number". The array pointed is used to build a view, each elements represent a subview to put in the view. Here is the code:

    for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
    NSLog(@"%d",c);
    switch (c) {
        case kForchettaPesce:   case kForchettaNormale:
        {
            NSString *imagePath;
            imagePath=[[NSString alloc]initWithFormat:@"%@/%d.png",[[NSBundle mainBundle]resourcePath],c];
            UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
            [imagePath release];
            NSLog(@"pippo");
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
            imageView.image=image;
            [image release];
            [self.view addSubview:imageView];
            [imageView release];

        break;
        }


        default:
        break;
    }
    NSLog(@"%d",(int)*composizione);



}

Debugging it I've found out that works perfectly untill it tries to add the subview. It seems to stay stucked in the loop and the log shows always the same datas in an infinite cycle. If I delete the -addSubview method I have no problem, the log statement shows what I expect to see. What am I doing wrong? Regards, Andrea

+1  A: 

I'm not sure what you are trying to do, but it may be that you're kind of mixing up your types:

for (self.composizione; *composizione>kFine; composizione++) {
    int c=(int)*composizione;
  • the self.composizione after for( is redundant, it doesn't do anything. Should it?
  • *composizione is the value pointed to by composizione, which should be an int* (is it?)
  • composizione++ advances the pointer to the next location in memory, not the value of *composizione

in short: maybe you need a (*composizione)++ instead of composizione++.

mvds
Hello mvds, I appreciate your suggestions. Sorry for the italian part in my code, but I forgot to translate.1°yes self.composizione is redudant, but i use for better reading|||2°yes, almost :-) in the reality is a pointer to a typedef enum|||3°right! I'm scrolling an array so it must increment the pointer, not the value|||Thanks anyway, but the solution was easier (as always).I was using that chunk of code in -loadView method that is not correct for view initialized in nib. I'm using a nib so I delete it and copied into viewDidLoad, everything is working now.
Andrea
Good that you solved it. You may consider writing it in such a way, that it is a little more self-explanatory. I.e. `for(int i=0;comp[i]!=kLast;i++) { }`. Especially if other people need to read it.
mvds