NSString init 或 alloc 错误?
我有这个接口:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您希望将
[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 youset*
methods as you're not releasing the older values prior to assigning the new one.在 getter 方法中,您将返回一个自动释放的对象,该对象可能从未被分配过。换句话说,您只分配 setter 中的对象。如果您希望对象以自动释放的方式返回,则必须先分配它们,然后以自动释放的方式返回它们。
您可能最好为每个 ivar 创建属性并让它们由类保留和释放。如果尚未分配它们,您仍然需要分配它们,您可能需要重写 getter 来执行此操作。像这样的东西。
在你的标头中,你将拥有
属性,
然后在实现中的
你将在你的 -(void)dealloc 中合成和释放
这样,在你创建一个 Card 类之后,你可以调用像
希望这样的东西,这会有所帮助。
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.
in your header you would have
and then the property
then in the implementation you would synthesize
and release in your -(void)dealloc
This way after you make a Card class you can call things like
Hope this helps.