objective-c [NSMutableDictionary setValue: value forKey: key] 是否保留 NSString 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/671322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:17:29  来源:igfitidea点击:

Does [NSMutableDictionary setValue: value forKey: key] retain NSString key?

objective-cmemory-management

提问by Alex

When adding items to NSMutableDictionaryusing the setValue:forKey:method (I suppose this generalizes to any NSObject) does the dictionary retain the second parameter, the NSString?

NSMutableDictionary使用该setValue:forKey:方法添加项目时(我想这可以推广到 any NSObject)字典是否保留第二个参数NSString?

For example:

例如:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSString *theString = @"hello";
int i;
for (i=0; i<[theString length]; i++){
    NSNumber *myInt = [NSNumber numberWithInt:i];
    NSString *character = [NSString stringWithFormat:@"%C",[theString characterAtIndex:i]];
    [dict setValue: myInt forKey:character];
}
[dict release];
[pool release];

Clearly, there is no reason to release myIntin the loop, it is retained by dictso it can't be released until the end of the code. But is the same true of character? My thinking is that if NSMutableDictionarystores the string in some other way, then one could create a temporary pool around the loop and release those strings instead of waiting until the release of the dictionary.

显然,myInt循环中没有理由释放,它被保留,dict所以直到代码结束才能释放。但同样如此character吗?我的想法是,如果NSMutableDictionary以其他方式存储字符串,那么可以在循环周围创建一个临时池并释放这些字符串,而不是等到字典的释放。

I am also curious as to why retainCountof characteris 7fffffff as if it is an NSConstantString, I would expect stringWithFormatto return an NSStringobject which would need retaining, but that doesn't seem to be the case.

我也很好奇,为什么retainCountcharacter是7FFFFFFF,就好像它是一个NSConstantString,我希望stringWithFormat返回一个NSString对象,它需要保留,但似乎并不如此。

回答by Alex

It's very common in Cocoa for NSStringparameters to be copied instead of retained. That's because you could have just as easily given the dictionary an instance of NSMutableString. Because the string's value could change, NSDictionarymakes a copy.

在 Cocoa 中,NSString参数被复制而不是保留是很常见的。那是因为你可以很容易地给字典一个NSMutableString. 因为字符串的值可能会改变,所以NSDictionary制作一个副本。

But, regardless of how NSMutableDictionaryreally operates, you don't have to worry whether characterneeds to be retained. Once you've passed it to NSMutableDictionaryas a parameter, it's really that class's problem to decide how to store the data, unless the documentation specifically tells you that retaining the objects are your responsibility.

但是,无论实际如何NSMutableDictionary运作,您都不必担心是否character需要保留。一旦您将它NSMutableDictionary作为参数传递给,那么决定如何存储数据确实是该类的问题,除非文档明确告诉您保留对象是您的责任。

I also wouldn't worry too much about the retainCountof any object. Following the retain count of an object too closely can lead you down rabbit holes that just make you spin your wheels.

我也不会太担心retainCount任何对象的 。太紧跟对象的保留计数会导致您陷入兔子洞,这只会让您旋转轮子。

Finally, I really don't think you need to create your own autorelease pool here. Unless you know with absolute certainty that theStringis going to be very long, or you've already observed high memory utilization in Instruments, adding the autorelease pool is an unnecessary optimization.

最后,我真的不认为你需要在这里创建自己的自动释放池。除非您绝对确定这theString会很长,或者您已经观察到 Instruments 中的高内存利用率,否则添加自动释放池是不必要的优化。

回答by Alex

You don't need to retain characterthere, the dictionary retains it when you set it as a key and your own code has no need to retain it.

您不需要保留character在那里,当您将其设置为键时,字典会保留它,而您自己的代码无需保留它。

You also don't need to worry about why the retain count isn't what you expect. Maybe the Foundation framework has Flyweight-like instances of a load of single-character NSStringinstances. In any case if you've got the memory management correct following the guidelines, you'll be OK regardless of what the framework's doing behind the scenes. http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html

您也不必担心为什么保留计数不是您所期望的。也许 Foundation 框架有大量单字符NSString实例的类似 Flyweight 的实例。在任何情况下,如果您按照指南正确地进行了内存管理,那么无论框架在幕后做什么,您都可以。http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html