@synthesize 创建什么 UIView setter

发布于 2024-11-03 08:39:47 字数 612 浏览 0 评论 0原文

如何制作自定义 UIView setter。例如:

1)我们创建属性:

@property (retain) IBOutlet UILabel *myLabel

2)我们创建setter(与@synthesize创建的相同):

- (void)setMyLabel:(UILabel *)anObject
{
     [myLabel release];
     myLabel = [anObject retain]; 
}

是否正确,或者我应该检查新视图是否与当前视图不同

- (void)setMyLabel:(UILabel *)anObject
{
    if(anObject != myView){
        [myLabel release];
        myLabel = [anObject retain]; 
    }
}

Just myView and anObject are object指针。那么我们应该用 -isEqual 方法检查它们吗?或者我们根本不需要检查它? @synthesize 默认生成什么代码?

谢谢。

How to make custom UIView setter. For example:

1) We create property:

@property (retain) IBOutlet UILabel *myLabel

2) we make setter (the same as @synthesize would create):

- (void)setMyLabel:(UILabel *)anObject
{
     [myLabel release];
     myLabel = [anObject retain]; 
}

Is it correct, or should I check if the new view are not the same as the current with

- (void)setMyLabel:(UILabel *)anObject
{
    if(anObject != myView){
        [myLabel release];
        myLabel = [anObject retain]; 
    }
}

Just myView and anObject are object pointers. So should we check them with -isEqual method then? Or we don't need to check it at all? What code does @synthesize generates by defaults?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有 2024-11-10 08:39:47

只有第二个版本(带有 if 语句)是正确的。在您的第一个版本中,假设 anObjectmyLabel 实际上指向同一个对象(即指针是相同的)。在这种情况下,您将释放该对象,如果没有其他对象保留该对象,这将导致该对象被释放。随后尝试保留已释放的对象将导致崩溃。

Only the second version (with the if statement) is correct. In your first version, imagine that anObject and myLabel actually point to the same object (i.e., the pointers are the same). In that case, you would release the object, which would cause it to be deallocated if no other object had retained it. The subsequent attempt to retain the deallocated object would cause a crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文