views:

249

answers:

2

This:

    UILable *myLabel = [[UILabel alloc] init];
    UILable *myLabel = [[UILabel alloc] init];

gives me a redefinition error.

But this:

    for(i=0;i<5;i++)
    { 
       UILable *myLabel = [[UILabel alloc] init];
       // some label code here
       [self.view addSubview:myLabel];
       [myLabel release];
    }

doesn't. So is the second one false? Should I define it before and just reuse it?

Is that right:

 UIIMageView *Sign;
//Some Sign Stuff
     Sign = [[UIImageView alloc]init];
        Sign.image = [UIImage imageNamed:@"Minus.png"]; 
        frame = CGRectMake(160 ,80, 64, 64);
        Sign.frame = frame;
        [scrollView addSubview:Sign];
        Sign = nil;
        [Sign release];
//Some other Sign stuff
     Sign = [[UIImageView alloc]init];
        Sign.image = [UIImage imageNamed:@"Plus.png"]; 
        frame = CGRectMake(200 ,80, 64, 64);
        Sign.frame = frame;
        [scrollView addSubview:Sign];
        Sign = nil;
        [Sign release];

is that correct? That doesnt work without the Sign = nil. So it seems a little wobbly too.

+1  A: 

You cannot have identical variable names used in the same block level scope. So in your first example you cannot have a variable definition with the same name, you have to name them differently.

- (void) method {
   UIImageView* image1;

   // here we define a new block scope. This can be a block of any kind (while, for, if)
   {
      // All reference in this block to this variable will see this definition.
      UIImageView* image1;

      // Using image1 here
   }

   // Here we see again the image1 defined at the beginning of the method.
}

In your loop example you are in a new scope that it's reinitialize after each iteration.

Your third example is correct in that it define the variable only one time. You reuse this variable after that to assign a new object. The third one is less elegant in that your variable name does not describe well for each case what are their purpose.

For your case of 'Sign = nil' this effectively make the line that follows useless since in Objective-C a message sent to a nil object is ignored.

I would suggest to define a method that you can call to create your images that look the same. Something like:

- (void) createImage:(NSString*) image frame:(CGRect) frame {
  UIIMageView *Sign;
  Sign = [[UIImageView alloc]init];
  Sign.image = [UIImage imageNamed:image]; 
  Sign.frame = frame;
  [self.scrollView addSubview:Sign];
  [Sign release];
}
Vincent
A: 

Your for-loop is perfectly fine. The scope of myLabel is limited to one run of your for-loop. So each run a new variable to hold the reference to your UILabel is created.

The second code you posted has leaks.

Sign = nil
[Sign release]

This will release the object at address nil and not the object you created. I can't see what else is wrong with your code, but your fix is definitely not fixing the root cause. Maybe it will help to post what error/warning you get when removing Sign = nil.

Also note that starting your variable names with a capital letter is not a good naming convention, because usually class names start with one.

sliver