English 中文(简体)
B. 重组国家安全局目录的内容 参考
原标题:resetting contents of NSMutableDictionary iVar

我有一个叫.的词典,这是一个 mu词。 有时,必须删除其内容并重新编排新内容。 其内容在几个无限期的频率上增加了,因此,这是不可信的。

Is it enough to "reset" or release the dictionary with:

 self.dictionaryIvar=nil;

然后,根据需要,采用不同的方法,用以下方法重新计算判词:

 if(!self.dictionaryIvar){
 self.dictionaryIvar = [NSMutableDictionary dictionary];
 }

这是否可接受,或者是否泄漏目标?

最佳回答

如果对贵财产的定义类似:

@property (nonatomic, readwrite, retain) NSMutableDictionary *dictionaryIvar;
//...
@synthesize dictionaryIvar;

然后,综合方法将注意释放/保留dict物。

self.dictionaryIvar = [NSMutableDictionary dictionary];

因此应该足够。

<条码>自订字典>至<条码> 前者通过综合财产法(-(NSMutableDictionary *) Ivar; and -(ave)setDictionaryIvar:NSMutableDictionary *aDictionary; each) and dictionaryIvar onlyaccesses the/2007/5.

因此,这将造成泄漏(和坠毁):

dictionaryIvar = [NSMutableDictionary dictionary];

虽然如此,

self.dictionaryIvar = [NSMutableDictionary dictionary];

但我(在大多数情况下)确实只接受这一简单呼吁:

[dictionaryIvar removeAllObjects];
问题回答

不是这样做,原因不仅仅是......

[dictionaryIvar removeAllObjects];

如果你想要改变字典的内容,你只能用新的字典取代字典。 一行:

<代码>[字典] Ivar setDictionary:[NSMutable] 字典];

这样做具有同样的效果:<代码>。 Ivar去除所有物体][(该编码还向所有物体发送<>release/code>),随后在新字典上添加物体(如你,空)。

此外,如果你需要做的话,你就不必担心你们的主人翁感和记忆管理问题。

假设您的<代码>@property被设定为retain,即应为罚款。 使用权责(<条码>本身.dictionaryIvar)将自动释放并保留你的字典。

If you re just trying to clear the dictionary, though, it might be better to just call the dictionary s -removeAllObjects method:

http://www.un.org/Depts/DGACM/index_french.htm

由于你正在使用<代码>本身<>的<>nil,,它将援引您的<代码>dictionaryIvar的机功能,然后公布先前持有的物体并指定nil

我倾向于在<代码>dictionaryIvar变量上适当发布。

  [dictionaryIvar release];
    dictionaryIvar = nil;

 if(!dictionaryIvar){
  dictionaryIvar = [[NSMutableDictionary dictionary] retain];
 }




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签