我需要使用postNotificationName:object:userInfo:
方法发布通知,并且我将一个自定义类FileItem
作为userInfo
传递,以便我可以在另一端获得它。我应该把它传递给默认的通知中心后使用autorelease
这样我是否需要在将对象作为用户数据发送到NotificationCenter之前自动释放对象?
FileItem *item = [[[FileItem alloc] init] autorelease];
[[NSNotificationCenter defaultCenter] postNotificationName:@"dataReceived" object:self userInfo:item];
[item release];
或可我只是alloc
然后release
物体马上会?
FileItem *item = [[FileItem alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"dataReceived" object:self userInfo:item];
[item release];
我想在这里拿到约定我认为每当我在消息传递一个对象作为参数传递给另一个对象,接收对象会做,如果它需要一个保留,而且我可以放心地释放该参数?
谢谢。现在对我来说更清楚了。 虽然当我在.h文件中声明NSString *时,我经常感到困惑,在dealloc方法中释放NSString *,但在init文件中没有alloc/copy/retain,只有像stringObject = [NSString stringWithFormat @ “.....”]; 你能告诉我在这种情况下发生了什么? – Ben 2009-06-04 06:01:37
在这种情况下,您不应该在dealloc方法中释放字符串:不分配/复制/保留,不释放。然而,你可能想要做的是这样的:stringObject = [[NSString stringWithFormat:@“...”] retain];然后在你的dealloc方法中释放stringObject。这可确保字符串在您准备好之前不会消失。 – 2009-06-04 06:15:19