我有以下代碼:EXC_BAD_ACCESS從通訊錄添加聯繫人時?
ABAddressBookRef ab;
ab = ABAddressBookCreate();
int len = (int) ABAddressBookGetPersonCount(ab);
int i;
for(i = 1; i < (len + 1); i++)
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
static char* fallback = "";
int fbLength = strlen(fallback);
int firstNameLength = fbLength;
bool firstNameFallback = true;
int lastNameLength = fbLength;
bool lastNameFallback = true;
if (firstName != NULL)
{
firstNameLength = (int) CFStringGetLength(firstName);
firstNameFallback = false;
}
if (lastName != NULL)
{
lastNameLength = (int) CFStringGetLength(lastName);
lastNameFallback = false;
}
if (firstNameLength == 0)
{
firstNameLength = fbLength;
firstNameFallback = true;
}
if (lastNameLength == 0)
{
lastNameLength = fbLength;
lastNameFallback = true;
}
firstNameString = malloc(sizeof(char)*(firstNameLength+1));
lastNameString = malloc(sizeof(char)*(lastNameLength+1));
if (firstNameFallback == true)
{
strcpy(firstNameString, fallback);
}
else
{
CFStringGetCString(firstName, firstNameString, 10*CFStringGetLength(firstName), kCFStringEncodingASCII);
}
if (lastNameFallback == true)
{
strcpy(lastNameString, fallback);
}
else
{
CFStringGetCString(lastName, lastNameString, 10*CFStringGetLength(lastName), kCFStringEncodingASCII);
}
printf("%d.\t%s %s\n", i, firstNameString, lastNameString);
NSString *fname= [NSString stringWithFormat:@"%s",firstNameString];
NSString *lname= [NSString stringWithFormat:@"%s",lastNameString];
[dict setValue:fname forKey:@"fname"];
[dict setValue:lname forKey:@"lname"];
[self.arrname addObject:[dict copy]];
if (firstName != NULL)
{
CFRelease(firstName);
}
if (lastName != NULL)
{
CFRelease(lastName);
}
free(firstNameString);
free(lastNameString);
}
它第一次運作良好。
但是,當我從聯繫人列表中刪除記錄,然後嘗試在以下語句中添加記錄我的應用程序清除。
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
任何人都可以解決這個問題嗎? 任何想法,不勝感激。
非常感謝..現在它工作得很好。大... – 2010-11-13 09:38:32