简体   繁体   English

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

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

When adding items to NSMutableDictionary using the setValue:forKey: method (I suppose this generalizes to any NSObject ) does the dictionary retain the second parameter, the NSString ?使用setValue:forKey:方法向NSMutableDictionary添加项目时(我想这可以推广到任何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 myInt in the loop, it is retained by dict so it can't be released until the end of the code.显然,没有理由在循环中释放myInt ,它由dict保留,因此直到代码结束才能释放。 But is the same true of character ?但是character也是这样吗? My thinking is that if NSMutableDictionary stores 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.我的想法是,如果NSMutableDictionary以其他方式存储字符串,那么可以在循环周围创建一个临时池并释放这些字符串,而不是等到字典释放。

I am also curious as to why retainCount of character is 7fffffff as if it is an NSConstantString , I would expect stringWithFormat to return an NSString object which would need retaining, but that doesn't seem to be the case.我也很好奇为什么characterretainCount是 7fffffff 就好像它是一个NSConstantString ,我希望stringWithFormat返回一个需要保留的NSString object ,但似乎并非如此。

It's very common in Cocoa for NSString parameters to be copied instead of retained.在 Cocoa 中, NSString参数被复制而不是保留是很常见的。 That's because you could have just as easily given the dictionary an instance of NSMutableString .那是因为你可以很容易地给字典一个NSMutableString的实例。 Because the string's value could change, NSDictionary makes a copy.因为字符串的值可能会改变,所以NSDictionary会创建一个副本。

But, regardless of how NSMutableDictionary really operates, you don't have to worry whether character needs to be retained.但是,无论NSMutableDictionary真正如何操作,您都不必担心character是否需要保留。 Once you've passed it to NSMutableDictionary as 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 ,决定如何存储数据确实是该类的问题,除非文档明确告诉你保留对象是你的责任。

I also wouldn't worry too much about the retainCount of any object.我也不会太担心任何retainCount的 retainCount。 Following the retain count of an object too closely can lead you down rabbit holes that just make you spin your wheels.按照 object 的保留计数太近可能会导致您掉入兔子洞,这只会让您旋转轮子。

Finally, I really don't think you need to create your own autorelease pool here.最后,我真的不认为你需要在这里创建自己的自动释放池。 Unless you know with absolute certainty that theString is 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 中的 memory 利用率很高,否则添加自动释放池是不必要的优化。

You don't need to retain character there, 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 NSString instances.可能 Foundation 框架具有类似 Flyweight 的加载单字符NSString实例的实例。 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.在任何情况下,如果您按照指南正确地管理了 memory,那么无论框架在幕后做什么,您都会没事的。 http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html http://iamleeg.blogspot.com/2008/12/cocoa-memory-management.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM