本文介绍了地址簿:尝试添加“家庭”和“工作”地址,仅显示1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在个人记录中添加住所和工作地址。似乎只有1个出现(以后添加的一个。是否可以向一个Person添加多个地址并看到它们显示在UnknownPersonViewController中?如果这样,我应该怎么做?

I'm trying to add a 'Home' and 'Work' address my Person record. It seems only 1 shows up (the one added later. Is if possible to add multiple addresses to a Person and see them displayed in the UnknownPersonViewController? If so, how should I do this?

这是我的代码:

void multiValueAddDictionaryValueAndLabel(ABMultiValueRef multi, CFDictionaryRef values, CFStringRef label) {
    if (multi && values != NULL) {
       ABMultiValueAddValueAndLabel(multi, values, label, NULL);          
    }               
}

CFStringRef getValueForKey(CFDictionaryRef dict, CFStringRef key) {
   CFStringRef value = NULL;

   if (CFDictionaryContainsKey(dict, key)) {
      value = CFDictionaryGetValue(dict, key);
   }

   return value;
}

ABRecordRef createPerson(CFDictionaryRef dict) {
   ABRecordRef person = ABPersonCreate();

   /*
    Add work address ...
    */

   ABMultiValueRef workAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
   NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
                               (NSString *)getValueForKey(dict, CFSTR("d:street")), (NSString *)kABPersonAddressStreetKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:postalcode")), (NSString *)kABPersonAddressZIPKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:l")), (NSString *)kABPersonAddressCityKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:st")), (NSString *)kABPersonAddressCityKey,
                               (NSString *)getValueForKey(dict, CFSTR("d:co")), (NSString *)kABPersonAddressCountryKey,
                               nil];
   multiValueAddDictionaryValueAndLabel(workAddress, (CFDictionaryRef)values, kABWorkLabel);
   ABRecordSetValue(person, kABPersonAddressProperty, workAddress, NULL);
   CFRelease(workAddress);

   /*
    Add home address ...
    */

   ABMultiValueRef homeAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
 values = [NSDictionary dictionaryWithObjectsAndKeys:
             (NSString *)getValueForKey(dict, CFSTR("d:homeStreet")), (NSString *)kABPersonAddressStreetKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homePostalCode")), (NSString *)kABPersonAddressZIPKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeCity")), (NSString *)kABPersonAddressCityKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeState")), (NSString *)kABPersonAddressCityKey,
             (NSString *)getValueForKey(dict, CFSTR("d:homeCountry")), (NSString *)kABPersonAddressCountryKey,
             nil];
   multiValueAddDictionaryValueAndLabel(homeAddress, (CFDictionaryRef)values, kABHomeLabel);
   ABRecordSetValue(person, kABPersonAddressProperty, homeAddress, NULL);
   CFRelease(homeAddress);
}     


推荐答案

您想要做的是对两个地址使用相同的可变ABMultiValueRef:

What you want to do is use the same mutable ABMultiValueRef for both addresses:

ABMultiValueRef addresses = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

// set up your 2 dictionaries here as you did in your question (though obviously with differing names)

ABMultiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)workValues, kABWorkLabel);
ABMultiValueAddDictionaryValueAndLabel(addresses, (CFDictionaryRef)homeValues, kABHomeLabel);

ABRecordSetValue(person, kABPersonAddressProperty, homeAddress, NULL);
CFRelease(addresses);

这篇关于地址簿:尝试添加“家庭”和“工作”地址,仅显示1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:01