类未按预期设置
图标设置为 @property (nonatomic, keep) AHGridIcon *icon;
通常我只是这样做:
-(void)setIcon:(AHGridIcon *)iconLocal {
icon = iconLocal;
}
但我在线阅读了 getters setters 和属性指南,这让我相信,这是正确的:
-(void)setIcon:(AHGridIcon *)iconLocal {
if (iconLocal != self.icon)
{
NSLog(@"local: %@", iconLocal);
NSLog(@"self.icon 1: %@", self.icon);
[iconLocal retain];
[icon release];
icon = iconLocal;
NSLog(@"self.icon 2: %@", self.icon);
}
}
问题是,原始图标保持不变,没有被新图标替换。我做错了什么?我应该恢复到通常的方式吗?
Icon is set as @property (nonatomic, retain) AHGridIcon *icon;
Usually i just do:
-(void)setIcon:(AHGridIcon *)iconLocal {
icon = iconLocal;
}
But i read a guide to getters setters and properties online which has lead me to believe that instead, this is right:
-(void)setIcon:(AHGridIcon *)iconLocal {
if (iconLocal != self.icon)
{
NSLog(@"local: %@", iconLocal);
NSLog(@"self.icon 1: %@", self.icon);
[iconLocal retain];
[icon release];
icon = iconLocal;
NSLog(@"self.icon 2: %@", self.icon);
}
}
The problem is, the original icon is staying put, it's not being replaced with the new icon. What am i doing wrong? Should i just revert to the usual way i do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用“@synthesize”,除非您确实需要自定义设置器行为。
You should use '@synthesize' unless you really need custom setter behavior.
就像我在评论中发布的那样:
最好的方法是使用
@synthesize
,它将根据您在属性中编写的属性创建一个 getter 和 setter(nonatomic,retain)< /代码> =>不是线程安全的,但快速 getter 和 setter 以及保留(也释放)setter。如果您不需要在设置器中执行复杂的操作,那么您不应该覆盖设置器。
.h:
.m:
您在设置器中发布的代码几乎与编译器仅使用合成时生成的代码相同。
您通常的方式并不是很好,因为在您的标头中定义了(在您的属性中)设置器正在保留,但在您的实现中您正在覆盖不保留的正确设置器。它几乎与编译器使用(非原子、分配)属性生成的结果相同。
但是,如果您想覆盖您的设置器,那么它应该看起来与您编写的相同。对我来说它工作得很好。
您甚至可以省略 if 但首先保留新对象,然后释放旧对象非常重要(就像您所做的那样 - 只是想要提一下)。
为了解决您使用覆盖设置器的问题:您的设置器在我眼中看起来不错。你也重写了 getter 吗?如果是,则将其发布在这里(您可以通过在日志调用中调用 self.icon 来使用它)。
我做了一个小测试程序
,输出很好:
like I posted in my comment:
the best way is to use
@synthesize
which will create a getter and a setter to with respect to the properties you wrote in your property(nonatomic, retain)
=> not threadsafe but fast getter and setter and a retaining (and also releasing) setter. If you dont need sophisticating stuff to do in your setter then you should not override the setter..h:
.m:
The code you posted in your setter is nearly the same as the compiler would produce when only using synthesize.
Your usual way is not really nice because in your header is defined (in your property) that the setter is retaining but in your implementation you are overriding that correct setter which doesn't retain. It is nearly the same as the compiler would produce with an (nonatomic, assign) property.
But if you want to override your setter then it should look like the same as you wrote. For me it is working fine.
you can even omit your if but then it is really important that you first retain the new and then release the old objects (like you did - just want to mention that).
For solving your problem with an overriten setter: Your setter looks ok in my eyes. Have you also overriten the getter? If yes then post it here (you use it by calling self.icon in your log-call).
I've done a small test-program
and the output is fine: