NSNotification 是否保留该对象?
我的问题是关于添加到 -postNotificationName:object: userInfo:
方法的对象。
NSNotification 是否保留该对象? (与 NSMutableDictionary 或 Array 类似)...意味着我可以在发布通知后释放该对象
下面是一个代码片段,可帮助描述我的问题...释放该对象是否有效。 Apple 文档的链接可能非常有帮助。
NSMutableDictionary *teamDictCopy = [self.teamDict mutableCopy];
[teamDictCopy setObject:[NSNumber numberWithInt:self.scrollViewIndex] forKey:@"imageIndex"];
if([self.statusButton.title isEqualToString:@"Completed"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"UnComplete" object:teamDictCopy userInfo:nil];
}
[teamDictCopy release];
My question is in regards the object that gets added to a -postNotificationName:object: userInfo:
method.
Does the NSNotification retain the object ?
(in a similar fashion to NSMutableDictionary or Array) ... meaning I can release the object after posting the notification
Below is a code snippet to help describe my question ... is it valid to release the object. A link to Apple documentation could be really helpful.
NSMutableDictionary *teamDictCopy = [self.teamDict mutableCopy];
[teamDictCopy setObject:[NSNumber numberWithInt:self.scrollViewIndex] forKey:@"imageIndex"];
if([self.statusButton.title isEqualToString:@"Completed"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"UnComplete" object:teamDictCopy userInfo:nil];
}
[teamDictCopy release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定该方法是否保留
object
和userInfo
参数,但实际上,这并不重要。我认为您可能会设想
NSNotificationCenter
正在创建这些通知并以异步方式广播它们,但事实并非如此,正如NSNotificationCenter
的文档中所述(请参阅<. a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000219-1265" rel= "noreferrer">NSNotificationCenter 类参考),通知同步发布:因此,在您的代码中,通知中心创建通知,然后通过默认中心广播它。已注册此通知名称和对象组合的任何对象都将接收该通知,然后执行它们在注册该通知时指定的选择器。然后,控件返回到发布通知的类。
换句话说,当您的代码到达
[teamDictCopy release]
行时,teamDictCopy
将已经被所有感兴趣的各方“使用”。所以,释放它应该不会有任何危险。只是关于约定的注释。一般来说,
object:
参数是发布通知的对象,userInfo:
参数是NSDictionary
额外信息。因此,通常情况下,您会按如下方式处理通知:I'm not sure if the
object
anduserInfo
parameters are retained by that method or not, but in practice, it shouldn't really matter.I think you may be envisioning that
NSNotificationCenter
is creating these notifications and broadcasting them in an asynchronous manner, but that isn't the case. As stated in the documentation forNSNotificationCenter
(see NSNotificationCenter Class Reference), notifications are posted synchronously:So, in your code, the notification center creates the notification and then broadcasts it through the default center. Any objects which have registered for this combination of notification name and object will receive the notification and then perform the selector they specified when they registered for that notification. Afterwards, the control returns to the class that posted the notification.
In other words, by the time your code gets to the
[teamDictCopy release]
line, theteamDictCopy
will already have been "used" by all of the interested parties. So, there shouldn't be any danger in releasing it.Just a note on conventions. Generally, the
object:
parameter is meant to be the object that is posting the notification, and theuserInfo:
parameter is meant for anNSDictionary
of extra information. So, normally, you would handle the notification like follows:是的 - 一旦该对象被设置为通知的对象,您就可以释放该对象。
你也可以子类化。
至于具体的文件/声明:我具体不记得了。
然而,当类型被识别为对象时,这是对象、它们的实例变量以及分布式通信和信令的基础。
我已经为你写了一个测试,所以你可以放心。如果不保留对象,通知的用例将会很少。只需在指示处添加断点,然后在启用断点的情况下运行即可。享受!
yes - you can release the object once it's been set as the notification's object.
you can also subclass.
as far as a specific document/statement: i don't remember one, specifically.
this is however the basis of objects, their instance variables, and distributed communications and signaling when types are identified as being an object.
i've written a test for you, so you can be assured of this. the use cases of notifications would be few if the object were not retained. just add a breakpoint where instructed, then run with breakpoints enabled. enjoy!