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 - 、および他のウェブページを一晩中、私は同様の問題を見つけることができませんでした..助けてください!あまり前もってありがとう!
多分コードを投稿し、あなたの質問を言い直して、私はあなたの質問やあなたのコードを理解していない場合はちょうど意味がない –
ありがとう、Xスラッシュ!私はstackoverflowを使用して、英語でプログラム上の問題を尋ねるために新しいです。私はすぐに私の変更します! –