Question

I am having issues with removeObjectsInArray returning an array with zero objects. The two arrays are different in size by 1 to begin with.

The code below is creating an array of all my contacts phone numbers.

        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleMutable,i);
        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
        mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            break ;
        }

    }

    [contactListNumbersArray addObject:[dOfPerson objectForKey:@"Phone"]];

I am then saving this array to the NSUserDefaults outside the function that pulls the phone numbers. So it doesn't save the phone numbers everytime immediately.

    userDefaultArray = contactListNumbersArray;
    [defaults setObject:userDefaultArray forKey:@"allContactsNumberArray"];

I then go into the address book on my phone and add a contact. When it comes back in it realizes that my saved NSUserDefault is no longer the same as my address book. It redefines contactListNumbersArray (not the NSUserDefault) through the code above.

I then wanted to determine exactly what had changed so this is the code I was using.

        NSMutableArray *tempDiffArray = [[NSMutableArray alloc] init];

        tempDiffArray = [NSMutableArray arrayWithArray:contactListNumbersArray];

        [tempDiffArray removeObjectsInArray:userDefaultArray];

I got this working for the address book names, but the phone numbers are giving me issues. For instance 1 array will be size 403, the other 402, and I end up with a zero array.

Was it helpful?

Solution

The method removeObjectsInArray: will remove multiple occurrences of the same value. That your code works for names but fails for phone numbers suggests you have two or more names with the same number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top