我是否必须保留/释放从 NSDictionary 获取的值?
通常我会做这样的事情(如下)来获取我需要的值:
NSDictionary *state = [notification.userInfo objectForKey:@"state"];
[self.tabBarItem setBadgeValue:[state objectForKey:@"short"]];
但是对于从 NSDictionary 获取的值是否值得保留/释放?像这样:
NSDictionary *state = [[notification.userInfo objectForKey:@"state"] retain];
[self.tabBarItem setBadgeValue:[state objectForKey:@"short"]];
[state release];
它对内存更友好吗?或者我不应该打扰并只信任自动释放池来完成它的工作?
Normally I do something like this (below) to get value I need:
NSDictionary *state = [notification.userInfo objectForKey:@"state"];
[self.tabBarItem setBadgeValue:[state objectForKey:@"short"]];
But does it worth to do retain/release for values taken from NSDictionary? Something like this:
NSDictionary *state = [[notification.userInfo objectForKey:@"state"] retain];
[self.tabBarItem setBadgeValue:[state objectForKey:@"short"]];
[state release];
Is it more memory friendly? Or I shouldn't bother and just trust to autorelease pool to do its job?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这样做没有意义。
[notification.userInfo objectForKey:@"state"]
的结果始终是一个自动释放的对象;如果您添加额外的retain
和release
,那不会改变任何东西。No, there's no point in doing that. The result of
[notification.userInfo objectForKey:@"state"]
is always an autoreleased object; if you add an extraretain
andrelease
, that won't change anything.