类未按预期设置

发布于 2024-11-28 06:06:38 字数 602 浏览 1 评论 0原文

图标设置为 @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 技术交流群。

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

发布评论

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

评论(2

‘画卷フ 2024-12-05 06:06:38

您应该使用“@synthesize”,除非您确实需要自定义设置器行为。

You should use '@synthesize' unless you really need custom setter behavior.

留蓝 2024-12-05 06:06:38

就像我在评论中发布的那样:

最好的方法是使用 @synthesize ,它将根据您在属性中编写的属性创建一个 getter 和 setter (nonatomic,retain)< /代码> =>不是线程安全的,但快速 getter 和 setter 以及保留(也释放)setter。如果您不需要在设置器中执行复杂的操作,那么您不应该覆盖设置器。

.h:

@property (nonatomic, retain) AHGridIcon *icon;

.m:

@implementation Something
@synthesize icon;
...
@end

您在设置器中发布的代码几乎与编译器仅使用合成时生成的代码相同。

您通常的方式并不是很好,因为在您的标头中定义了(在您的属性中)设置器正在保留,但在您的实现中您正在覆盖不保留的正确设置器。它几乎与编译器使用(非原子、分配)属性生成的结果相同。

但是,如果您想覆盖您的设置器,那么它应该看起来与您编写的相同。对我来说它工作得很好。

  1. 首先保留新对象,
  2. 然后释放旧对象
  3. ,然后将本地指针分配给新对象,

您甚至可以省略 if 但首先保留新对象,然后释放旧对象非常重要(就像您所做的那样 - 只是想要提一下)。

为了解决您使用覆盖设置器的问题:您的设置器在我眼中看起来不错。你也重写了 getter 吗?如果是,则将其发布在这里(您可以通过在日志调用中调用 self.icon 来使用它)。

我做了一个小测试程序

@synthesize str;

- (void)setStr:(NSString *)localStr
{
    if(str != localStr)
    {
        NSLog(@"old : %@", self.str);
        NSLog(@"new1: %@", localStr);
        [localStr retain];
        [str release];
        str = localStr;

        NSLog(@"new2: %@", self.str);
    }
}

,输出很好:

old : (null)
new1: Hello
new2: Hello

old : Hello
new1: World
new2: World

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:

@property (nonatomic, retain) AHGridIcon *icon;

.m:

@implementation Something
@synthesize icon;
...
@end

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.

  1. first retaining the new object
  2. then releasing the old one
  3. then assigning the local pointer to your new object

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

@synthesize str;

- (void)setStr:(NSString *)localStr
{
    if(str != localStr)
    {
        NSLog(@"old : %@", self.str);
        NSLog(@"new1: %@", localStr);
        [localStr retain];
        [str release];
        str = localStr;

        NSLog(@"new2: %@", self.str);
    }
}

and the output is fine:

old : (null)
new1: Hello
new2: Hello

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