Pregunta

I am trying to make an Array of dictionaries but something going wrong I have a mistake (logical) in my code and I can't figure out it for 2 hours, (I am new to iOS development , so maybe some experienced eye can see it fast?)

here is my code

Edited: code Update

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    //get all contacts names
    NSMutableArray* EmailArrayPerPerson = [[NSMutableArray alloc]init];
    NSMutableArray* PhoneArrayPerPerson = [[NSMutableArray alloc]init];
    for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++)
    {
        NSMutableDictionary *ContactsDetails = [NSMutableDictionary dictionary];
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
        NSString *contact = (__bridge NSString *)(ABRecordCopyCompositeName(ref));
        NSLog(@"%@",contact);//contact is good here
        ContactsDetails = [NSMutableDictionary dictionary];
        [ContactsDetails setObject:contact forKey:@"CName"];
        ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
        [EmailArrayPerPerson removeAllObjects];
        NSLog(@"dictionary is:%@",[ContactsDetails objectForKey:@"CName"]);

        for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++)
        {
            NSString* email = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(emails, j));
            [EmailArrayPerPerson addObject:email];
        }
        [ContactsDetails  setObject:EmailArrayPerPerson forKey:@"CEMails"];

        ABMultiValueRef multi = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        [PhoneArrayPerPerson removeAllObjects];
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++)
        {
            NSString* phone = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(multi, j));
            [PhoneArrayPerPerson addObject:phone];
        }
         [ContactsDetails setObject:PhoneArrayPerPerson forKey:@"CPhones"];
        NSLog(@"dictionary is:%@",ContactsDetails);
        [Contacts addObject:ContactsDetails];
        [Contacts insertObject:ContactsDetails atIndex:i];
         NSLog(@"%@",Contacts);
    }
NSLog(@"%d",[Contacts count]);

Contacts is my mutableArray, but when I am printing it it always has the values of the last dictionary at all of his indexes.

when I am printing the dictionary in each iteration , I get the correct values inside. Please give it a look help me to figure out what is wrong there.

I did mix of all answers , now it is partially working , I getting distinct Names , but mails and phones are still same for all

¿Fue útil?

Solución 2

Try replacing each "remove object call," for example

[ContactsDetails removeAllObjects];
[EmailArrayPerPerson removeAllObjects];

with a re-instantiation, i.e.

ContactDetails = [NSMutableDictionary dictionary];
EmailArrayPerPerson = [[NSMutableArray alloc]init];

because you're essentially placing the same objects in each slot in your array repeatedly. You need to create new objects for each iteration.

Otros consejos

You're only ever creating one dictionary, and then modifying it in every iteration. Your final result contains the same dictionary N times, not N copies of the last dictionary.

You're doing this, basically:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSMutableArray *dictionaries = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
   dict[@"something"] = @(i);
   [dictionaries addObject:dict]; // adding the same dictionary you added last time
}

You want to be doing this:

NSMutableArray *dictionaries = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
   NSMutableDictionary *dict = [NSMutableDictionary dictionary];
   dict[@"something"] = @(i);
   [dictionaries addObject:dict]; // adding the newly created dictionary
}

Side note...don't name your variables in Uppercase. In Objective-C, uppercase identifiers are used for class names. It's not technically wrong, but it's against the standard coding conventions for the language, which makes the code harder to read for other developers.

Put this line of code in side your for loop

NSMutableDictionary *ContactsDetails = [NSMutableDictionary dictionary];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top