1

次のコードでは:

//anArray is a Array of Dictionary with 5 objs. 

//here we init with the first
NSMutableDictionary *anMutableDict = [[NSMutableDictionary alloc] initWithDictionary:[anArray objectAtIndex:0]];

... use of anMutableDict ...
//then want to clear the MutableDict and assign the other dicts that was in the array of dicts

for (int i=1;i<5;i++) {
    [anMutableDict removeAllObjects];
    [anMutableDict initWithDictionary:[anArray objectAtIndex:i]];
} 

なぜこのクラッシュ?nsmutabledictをクリアし、新しいdictを割り当てる正しい方法はどのようになっていますか?

みんなありがとう。

マルコス。

4

3 に答える 3

3

オブジェクトを「再開」することはありません—これまでに。初期化は、新しくalloc編集されたインスタンスで使用されることを意図しており、初期化が完了した後は正しくない仮定を行う場合があります。NSMutableDictionaryの場合、setDictionary:ディクショナリの内容を新しいディクショナリに完全に置き換えるかaddEntriesFromDictionary:、別のディクショナリからエントリを追加するために使用できます(競合がない限り、現在のエントリを削除することはありません)。

mutableCopyより一般的には、その辞書を解放して、配列内の辞書を作成することができます。

于 2010-03-17T03:29:16.410 に答える
2

自動リリースされた辞書を使用すると、コードははるかに単純になります。

NSMutableDictionary *anMutableDict = [NSMutableDictionary dictionaryWithDictionary:[anArray objectAtIndex:0]];

... use of anMutableDict ...

for (int i=1; i<5; i++)
{
    anMutableDict = [NSMutableDictionary dictionaryWithDictionary:[anArray objectAtIndex:i]];
} 

しかし、私はあなたがそこの最後に持っているそのループのポイントを見ていません。

于 2010-03-17T03:13:57.173 に答える
1

これは、init/allocの使用方法ではありません。代わりに、次を試してください。

//anArray is a Array of Dictionary with 5 objs. 

//here we init with the first
NSMutableDictionary *anMutableDict = [[NSMutableDictionary alloc] initWithDictionary:[anArray objectAtIndex:0]];

... use of anMutableDict ...
//then want to clear the MutableDict and assign the other dicts that was in the array of dicts

for (int i=1;i<5;i++) {
    [anMutableDict removeAllObjects];
    [anMutableDict addEntriesFromDictionary:[anArray objectAtIndex:i]];
} 
于 2010-03-17T03:24:43.543 に答える