2011-11-17 55 views
0

我使用的XCode 4.2來開發功能將聯繫人添加到地址簿,這裏是我的代碼通訊簿實施

ABAddressBookRef *iPhoneAddressBook = ABAddressBookCreate(); 
ABRecordRef contact = ABPersonCreate(); 

//add infos 
ABRecordSetValue(contact, kABPersonFirstNameProperty,(__bridge_retained CFStringRef)firstName, nil); 
ABRecordSetValue(contact, kABPersonLastNameProperty,(__bridge_retained CFStringRef)lastName, nil); 
ABRecordSetValue(contact, kABPersonOrganizationProperty, (__bridge_retained CFStringRef)organization, nil); 
ABRecordSetValue(contact, kABPersonJobTitleProperty, (__bridge_retained CFStringRef)title, nil); 


ABMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiRealPropertyType); 


ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL); 
ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workFax, kABPersonPhoneWorkFAXLabel, NULL); 

ABRecordSetValue(contact, kABPersonPhoneProperty, multiPhone, nil); 
CFRelease(multiPhone); 

ABMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); 

ABMultiValueAddValueAndLabel(multiEmail, (__bridge_retained CFStringRef)email, kABWorkLabel, NULL); 

ABRecordSetValue(contact, kABPersonEmailProperty, multiEmail, nil); 

CFRelease(multiEmail); 
// address 

ABMultiValueRef multiAddress =ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); 
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc]init]; 
[addressDict setObject:address forKey:(NSString *) kABPersonAddressStreetKey]; 
[addressDict setObject:city forKey:(NSString *) kABPersonAddressCityKey]; 
[addressDict setObject:province forKey:(NSString *) kABPersonAddressStateKey]; 
[addressDict setObject:postalCode forKey:(NSString *) kABPersonAddressZIPKey]; 
[addressDict setObject:address forKey:(NSString *) kABPersonAddressCountryKey]; 

ABMultiValueAddValueAndLabel(multiAddress, (__bridge_retained CFStringRef)addressDict, kABWorkLabel, NULL); 
ABRecordSetValue(contact, kABPersonAddressProperty, multiAddress, NULL); 


CFRelease(multiAddress); 

ABMultiValueRef multiURL =ABMultiValueCreateMutable(kABMultiRealPropertyType); 
ABMultiValueAddValueAndLabel(multiURL, (__bridge_retained CFStringRef)link, kABPersonURLProperty, NULL); 
CFRelease(multiURL); 



ABAddressBookAddRecord(iPhoneAddressBook, contact, nil); 

BOOL didAdd = ABAddressBookSave(iPhoneAddressBook, nil); 

CFRelease(contact); 
CFRelease(iPhoneAddressBook); 

//notifying the user that it was stored in his address book 
if (didAdd) { 


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" 
message:@"Contact Info successfully added to the Address Book" 
delegate:self 
cancelButtonTitle:@"OK" 
otherButtonTitles:nil]; 
[alert show]; 
} 

程序編譯和,但它停止在這一行:

ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABPersonPhoneMainLabel, NULL); 

我得到這個錯誤(綠色)

Thread 1 

任何線索?代碼中出了什麼問題?

回答

0

這其中發生的錯誤的函數的定義:

bool ABMultiValueAddValueAndLabel (
    ABMutableMultiValueRef multiValue, 
    CFTypeRef value, 
    CFStringRef label, 
    ABMultiValueIdentifier *outIdentifier 
); 

您似乎切換第二和第三參數,對不對?