2012-01-25 3 views
2
for (int cnt = 0 ; cnt < nPeople ; cnt++) 
{ 
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, cnt); 

    NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
    NSString *lastName = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty); 
    NSString *fullName; 

    /* skipped code at here : code to merge firstName and lastName to fullName. In my country, many of us don't separate first name and last name */ 

    // tempKeyString : NSString variable that has key of fullNameArray value for nameDictionary. 
    // fullNameArray : to keep some fullName variables for tempKeyString. 
    if (!tempKeyString) // there's no tempKeyString, a.k.a. it's the first fullName. 
    { 
     // it's not important to know about GetUTF8String:fullName. It's for my own language. 
     tempKeyString = [self GetUTF8String:fullName]; 
     [fullNameArray addObject:fullName]; 
    } 
    else 
    { 
     if ([tempKeyString characterAtIndex:0] == [[self GetUTF8String:fullName] characterAtIndex:0]) // if fullName has the same tempKey with fullNameArray. 
     { 
      [fullNameArray addObject:fullName]; 
     } 
     else // if fullName has different tempKey with fullNameArray. 
     { 
      //tempKey : key data for fullNameArray 
      NSString *tempKey = [tempKeyString substringToIndex:1]; 
      // tempDict : to keep the deep copy of nameDictionary before adding new key. 
      NSDictionary *tempDict = [nameDictionary mutableDeepCopy]; 
      // add new key (tempKey) with new value (fullNameArray) 
      // PROBLEM : ALL values (including previous values) in dictionary(nameDictionary) are overwritten to a new value(fullNameArray). 
      [nameDictionary setObject:fullNameArray forKey:tempKey]; 

      //empties fullNameArray so that it can get the new fullName of the new tempKey. 
      [fullNameArray removeAllObjects]; 
      //refresh tempKeyString, and add the new fullName. 
      tempKeyString = [self GetUTF8String:fullName]; 
      [fullNameArray addObject:fullName]; 
      ... 
     } 
    } 
} 

内のすべてのデータが上書きされます。なぜNSMutableDictionary型オブジェクトを作成するのかは、私は連絡先のインデックスが必要であり、ABAddressRef型付きオブジェクトから直接インデックスを作るのは簡単ではありません。私も関数を検索する必要があります。のsetObject:forKey:NSMutableDictionaryのは、私は私のiPhoneの連絡先からNSMutableDictionaryオブジェクトを作成しようとしている辞書

私はちょうどコード化しても問題はありませんでしたが、デバッグ後に唯一の問題は私を狂ってしまいます。 tempNameという名前のキーを持つfullNameArrayという配列をnamedDictionaryに適用すると、nameDictionaryにfullNameArrayの値がすべて含まれていることがわかります。 以前のデータはすべて上書きされました! fullNameArrayを適用して新しいnameDictionaryにコピーする前に、以前のnameDictionaryのディープコピーバージョンを作成しようとしました。しかし、私が3行目でブレークポイントをチェックすると、tempDictで前のデータを見つけることができません。

コードとコメントを追加しました。それは私の説明以上に役立つかもしれません..質問は満足しています!

私はここから理由を見つけようとしました - StackOverflow - 、および他のウェブページを一晩中、私は同様の問題を見つけることができませんでした..助けてください!あまり前もってありがとう!

+1

多分コードを投稿し、あなたの質問を言い直して、私はあなたの質問やあなたのコードを理解していない場合はちょうど意味がない –

+0

ありがとう、Xスラッシュ!私はstackoverflowを使用して、英語でプログラム上の問題を尋ねるために新しいです。私はすぐに私の変更します! –

答えて

2

ここ

[nameDictionary setObject:fullNameArray forKey:tempKey]; 

は、あなたが効果的に、そして

[fullNameArray removeAllObjects]; 

、オブジェクト「fullNameArray」を使用して辞書を設定し、この配列内のすべての値を削除するので、それが空に取得する理由は、 "nameDictionary"内のオブジェクトを削除すると、それらは同じオブジェクトであり、辞書内に格納されているfullNameArrayのディープコピーではありません。とにかく配列に何かを格納する必要があるのはなぜですか?あなたは1つの値だけを保存しています。

[nameDictionary setObject:fullName forKey:tempKey]; 

は必要な処理を行います。申し訳ありませんが、私はあなたの質問を間違えた場合、それは非常に理解することは難しいです

+0

私はあなたの答えからヒントを得ることができました!私はsetObjectを知らなかった:forkey:深いコピーではなく、浅いコピーを使用する!だから、毎回fullNameArrayの深いコピーを作成し、setObject:forkey:のためにそれらを使用しました。今すぐ辞書には私が望むデータが表示されます!あなたの助けとアイデアをありがとう! p.s. 1つのtempKeyには多くの値があるので、それらを1つの配列変数に結合する必要があります。だから、私はfullNameArrayを使って多くのfullName値を結合しました。 –

関連する問題