本文介绍了[NSMutableDictionary setValue:Key的值:键]是否保留NSString键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用setValue:forKey:方法将项目添加到NSMutableDictionary时(我认为这可以推广到任何NSObject),字典是否保留第二个参数NSString?

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?

例如:

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];

很显然,没有理由在循环中释放myInt,它被dict保留,因此直到代码结束才可以释放.但是character也是一样吗?我的想法是,如果NSMutableDictionary以其他方式存储字符串,则可以在循环周围创建一个临时池并释放这些字符串,而不必等到字典发布后再使用.

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. But is the same true of 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.

我也很好奇为什么characterretainCount是7fffffff,就像它是NSConstantString一样,我希望stringWithFormat返回一个需要保留的NSString对象,但这不是.似乎是这样.

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.

推荐答案

在Cocoa中,通常复制NSString参数而不是保留参数.那是因为您可以很容易地为字典提供NSMutableString的实例.由于字符串的值可能会更改,因此NSDictionary会进行复制.

It's very common in Cocoa for NSString parameters 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, NSDictionary makes a copy.

但是,无论NSMutableDictionary的实际运行方式如何,您都不必担心是否需要保留character.将其作为参数传递给NSMutableDictionary后,决定如何存储数据确实是该类的问题,除非文档明确告知您保留对象是您的责任.

But, regardless of how NSMutableDictionary really operates, you don't have to worry whether character needs to be retained. 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.

对于任何对象的retainCount我也不会太担心.过于精确地跟踪对象的保留数会导致您掉进兔子洞,而这只会使您旋转轮子.

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

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

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.

这篇关于[NSMutableDictionary setValue:Key的值:键]是否保留NSString键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:51