如何重用UILabel? (或任何对象)

时间:2021-01-20 12:01:49

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.

那是对的吗?没有Sign = nil,这不起作用。所以看起来有点不稳定。

2 个解决方案

#1


1  

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.

对于'Sign = nil'的情况,这有效地使得后面的行无用,因为在Objective-C中,忽略了发送到nil对象的消息。

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];
}

#2


0  

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.

你的for循环非常好。 myLabel的范围仅限于你的for循环的一次运行。因此每次运行一个新变量来保存对UILabel的引用。

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.

这将释放地址为nil的对象,而不是您创建的对象。我看不出你的代码还有什么问题,但你的修复肯定没有解决根本原因。也许它有助于发布删除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.

另请注意,使用大写字母开始变量名称不是一个好的命名约定,因为通常类名称以1开头。

#1


1  

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.

对于'Sign = nil'的情况,这有效地使得后面的行无用,因为在Objective-C中,忽略了发送到nil对象的消息。

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];
}

#2


0  

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.

你的for循环非常好。 myLabel的范围仅限于你的for循环的一次运行。因此每次运行一个新变量来保存对UILabel的引用。

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.

这将释放地址为nil的对象,而不是您创建的对象。我看不出你的代码还有什么问题,但你的修复肯定没有解决根本原因。也许它有助于发布删除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.

另请注意,使用大写字母开始变量名称不是一个好的命名约定,因为通常类名称以1开头。