0

我正在向我的地址簿添加多个地址,但是当我尝试插入多个地址时,例如首先我将插入工作地址,然后如果我插入家庭地址,代码将插入家庭地址并删除工作地址.

这是我的代码:

NSArray *mainComponents = [line componentsSeparatedByString:@":"];
NSArray *components = [[mainComponents objectAtIndex:1] componentsSeparatedByString:@";"];

if ([line rangeOfString:@"Work"].location != NSNotFound) 
{
    NSLog(@"Work--------");
    ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    NSString *otherStreetAddress=[NSString stringWithFormat:@"%@",[components objectAtIndex:0] ];

    [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:2] forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:3] forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary2 setObject:[components objectAtIndex:4] forKey:(NSString *)kABPersonAddressStateKey];
    [addressDictionary2 setObject:[components objectAtIndex:5] forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary2 setObject:[components objectAtIndex:6] forKey:(NSString *)kABPersonAddressCountryKey];

    ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABWorkLabel, NULL);

    [addressDictionary2 release];

    ABRecordSetValue(personRecord, kABPersonAddressProperty,multiOther , NULL);// (personRecord, kABPersonAddressProperty, multiOther, NULL);

    CFRelease(multiOther);

        //ABAddressBookAddRecord(addressBook, personRecord, NULL);
}
else if ([line rangeOfString:@"HOME"].location != NSNotFound) 
{
    NSLog(@"Home0--------");

    ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    NSString *otherStreetAddress=[NSString stringWithFormat:@"%@",[components objectAtIndex:0] ];

    [addressDictionary2 setObject:otherStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:2] forKey:(NSString *)kABPersonAddressStreetKey];
    [addressDictionary2 setObject:[components objectAtIndex:3] forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary2 setObject:[components objectAtIndex:4] forKey:(NSString *)kABPersonAddressStateKey];
    [addressDictionary2 setObject:[components objectAtIndex:5] forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary2 setObject:[components objectAtIndex:6] forKey:(NSString *)kABPersonAddressCountryKey];

    ABMultiValueAddValueAndLabel(multiOther, addressDictionary2, kABHomeLabel, NULL);

    [addressDictionary2 release];

    ABRecordSetValue(personRecord, kABPersonAddressProperty,multiOther , NULL);// (personRecord, kABPersonAddressProperty, multiOther, NULL);

    CFRelease(multiOther);
}

ABAddressBookAddRecord(addressBook,personRecord, NULL);

如何插入多个地址?

4

1 回答 1

0

是的,我自己有解决方案

我需要添加以下行

ABMutableMultiValueRef multiOther = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

ABMultiValueRef immutableMultiEmail = ABRecordCopyValue(personRecord, kABPersonAddressProperty);

if (immutableMultiEmail) 

{

    multiOther= ABMultiValueCreateMutableCopy(immutableMultiEmail);

} 
else 

{

    multiOther = ABMultiValueCreateMutable(kABMultiStringPropertyType);
}

用于为同一字段创建新参考。现在工作完美......

于 2011-04-22T11:06:27.613 回答