objective-c 如何清除字典?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987165/
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 22:41:01  来源:igfitidea点击:

How to clear a dictionary?

iphoneobjective-ccocoa-touchnsmutabledictionary

提问by 4thSpace

I'm currently doing the following to clear out an NSMutableDictionary

我目前正在执行以下操作以清除 NSMutableDictionary

[myDictionary release];
myDictionary = [[NSMutableDictionary alloc] init];

The release line doesn't actually release any objects in the dictionary. I can still see all of them on the next line. It isn't until the alloc line is executed that the dictionary is zeroed out. Is there a better way to do it?

释放行实际上并没有释放字典中的任何对象。我仍然可以在下一行看到所有这些。直到执行 alloc 行,字典才被清零。有没有更好的方法来做到这一点?

回答by bbum

You say:

你说:

The release line doesn't actually release any objects in the dictionary. I can still see all of them on the next line. It isn't until the alloc line is executed that the dictionary is zeroed out. Is there a better way to do it?

释放行实际上并没有释放字典中的任何对象。我仍然可以在下一行看到所有这些。直到执行 alloc 行,字典才被清零。有没有更好的方法来做到这一点?

If -releaseof the mutable dictionary causes the dictionary to be deallocated -- drops the retain count to zero -- then the mutable dictionary will release all contained objects. Always.

如果-release可变字典的 导致字典被释放——将保留计数降至零——那么可变字典将释放所有包含的对象。总是。

Thus, if the objects aren't being released, then this suggested "fix"...

因此,如果对象没有被释放,那么这个建议的“修复”......

[myDictionary removeAllObjects];
[myDictionary release];
myDictionary = [[NSMutableDictionary alloc] init];

... is a memory leak in that the original instance of NSMutableDictionary will be leaked.

... 是内存泄漏,因为 NSMutableDictionary 的原始实例将被泄漏。

As a result, calling -removeAllObjectswillempty the dictionary and release all of the contained objects, but you still have a memory leak that you should figure out and fix.

因此,调用-removeAllObjects清空字典并释放所有包含的对象,但您仍然存在内存泄漏,您应该找出并修复它。



To be blunt:

坦率地说:

If the objects in your dictionary are not being deallocated when the dictionary receives the above -release(without calling -removeAllObjects), then there is a memory leak. It is either:

如果在字典接收到上述内容时-release(没有调用-removeAllObjects),字典中的对象没有被释放,则存在内存泄漏。它是:

  1. The objects in the dictionary have been retained an extra time.

  2. There is still an outstanding -retainon the dictionary.

  1. 字典中的对象被保留了额外的时间。

  2. -retain字典上还有一个突出 的。

Since you say the objects are being correctly (as in, as expected) deallocated when you call -removeAllObjects, then it must be (2). Look through your code and figure out where there is an extra -retainof the dictionary. You can use the Object Allocinstrument to figure out exactly where all of the retains come from.

既然您说对象在调用 时被正确(如预期的那样)释放-removeAllObjects,那么它必须是(2)。查看您的代码并找出-retain字典的额外内容。您可以使用Object Alloc工具来确定所有保留的确切来源。

回答by Ben Gottlieb

-removeAllObjects will work, as Andy mentioned. However, if any of your dictionary's objects are auto-released, or if the dictionary itself has been autoreleased somewhere along the line, then they won't be dealloc'd until the next time the runloop finishes (when the autorelease pool is drained).

-removeAllObjects 将起作用,正如安迪提到的那样。但是,如果您的字典中的任何对象是自动释放的,或者字典本身已在该行的某处自动释放,那么在下一次运行循环完成之前(当自动释放池耗尽时)它们不会被释放.

回答by Dan

[aMutableDictionary removeAllObjects];

from Apple's documentation:

来自苹果的文档

(void)removeAllObjects

Description - Empties the dictionary of its entries. Each key and corresponding value object is sent a release message.

Availability - iOS (2.0 and later)

(void)removeAllObjects

描述 - 清空其条目的字典。每个键和相应的值对象都会发送一条释放消息。

可用性 - iOS(2.0 及更高版本)