NSString init 或 alloc 错误?

发布于 2024-11-10 18:00:14 字数 850 浏览 0 评论 0原文

我有这个接口:

 #import <Foundation/Foundation.h>
 @interface Cards : NSObject {  NSString* effect;   NSString* image;   }
-(NSString*) effect;
-(NSString*) image;
-(void) setEffect: (NSString*) effect2;
-(void) setImage: (NSString*) image2;

@end

和这个实现:

#import "Cards.h"
@implementation Cards
-(NSString*) effect
{
    return [effect autorelease];
}
-(NSString*) image
{
    return [image autorelease];
}
-(void) setEffect: (NSString*) effect2
{
  effect = [[NSString alloc]initWithString:effect2];
}
-(void) setImage: (NSString*) image2
{
  image = [[NSString alloc]initWithString:@""];
}
-(void) dealloc
{
    [effect release];
    [image release];
    [super dealloc];
}
@end

现在,如果我制作一个 Cards 对象,例如 卡片*卡片 然后我像这样运行 setEffect 方法: [卡片设置效果:@""]; 它编译但给我一个运行时错误。有人知道为什么吗?提前致谢!

I have this interface:

 #import <Foundation/Foundation.h>
 @interface Cards : NSObject {  NSString* effect;   NSString* image;   }
-(NSString*) effect;
-(NSString*) image;
-(void) setEffect: (NSString*) effect2;
-(void) setImage: (NSString*) image2;

@end

And this implementation:

#import "Cards.h"
@implementation Cards
-(NSString*) effect
{
    return [effect autorelease];
}
-(NSString*) image
{
    return [image autorelease];
}
-(void) setEffect: (NSString*) effect2
{
  effect = [[NSString alloc]initWithString:effect2];
}
-(void) setImage: (NSString*) image2
{
  image = [[NSString alloc]initWithString:@""];
}
-(void) dealloc
{
    [effect release];
    [image release];
    [super dealloc];
}
@end

Now if I make a Cards object such as
Cards* card
and then I run the metod setEffect like so:
[card setEffect:@""];
It compiles but gives me a runtime error. Anyone know why? Thanks in advance!

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

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

发布评论

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

评论(2

鱼窥荷 2024-11-17 18:00:14

我认为您希望将 [image autorelease] 设为 [[image keep] autorelease]。否则,您将在每次访问该属性时自动释放它。您还泄漏了 set* 方法,因为您在分配新值之前没有释放旧值。

I think you intended [image autorelease] to be [[image retain] autorelease]. Otherwise you will be autoreleasing it on every access of the property. You are also leaking on you set* methods as you're not releasing the older values prior to assigning the new one.

雪落纷纷 2024-11-17 18:00:14

在 getter 方法中,您将返回一个自动释放的对象,该对象可能从未被分配过。换句话说,您只分配 setter 中的对象。如果您希望对象以自动释放的方式返回,则必须先分配它们,然后以自动释放的方式返回它们。

您可能最好为每个 ivar 创建属性并让它们由类保留和释放。如果尚未分配它们,您仍然需要分配它们,您可能需要重写 getter 来执行此操作。像这样的东西。

-(NSString *)effect {
    if (!effect) {
        effect = [[NSString alloc] init];
    }
return effect;

在你的标头中,你将拥有

NSString *effect;

属性,

@property(nonatomic, retain) NSString *effect;

然后在实现中的

@synthesize effect;

你将在你的 -(void)dealloc 中合成和释放

[effect release];

这样,在你创建一个 Card 类之后,你可以调用像

card.effect = @"Whatever goes in the effect property";

//assuming card is a Card object

希望这样的东西,这会有所帮助。

In your getter methods, you are returning an autoreleased object which may have never been allocated to begin with. In other words, you are only allocating the objects in your setter. If you want your objects to be returned autoreleased, you have to allocate them first, and then return them autoreleased.

You may be better off to create properties for each ivar and have them retained and released by the class. You would still need to allocate them, if they have not been allocated, which you may want to override the getter to do. Something like this.

-(NSString *)effect {
    if (!effect) {
        effect = [[NSString alloc] init];
    }
return effect;

in your header you would have

NSString *effect;

and then the property

@property(nonatomic, retain) NSString *effect;

then in the implementation you would synthesize

@synthesize effect;

and release in your -(void)dealloc

[effect release];

This way after you make a Card class you can call things like

card.effect = @"Whatever goes in the effect property";

//assuming card is a Card object

Hope this helps.

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