从地址簿添加联系人时 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);
谁能解决这个问题吗? 任何想法都将受到高度赞赏。
I have the following code :
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);
}
it working well for first time.
But When i delete record from contact list and then try to add record my App crase at the following statement.
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
Can anyone solve this problem ??
Any Idea is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法保证有效的
ABRecordID
从1
开始并以ABAddressBookGetPersonCount(addressBook)
结束。您不能使用ABRecordID
来使用 for 循环。相反,使用
ABAddressBookCopyArrayOfAllPeople
获取包含所有人员的CFArray
并对其进行迭代。另一个评论是你不应该使用 C 字符串;大多数事情都可以使用
CFString
和NSString
的 API 来完成,它们支持开箱即用的 Unicode。通过获取指定kCFStringEncodingASCII
的 C 字符串,您基本上会破坏诸如 é 或 ü、ગુજરાતી 或 case 之类的字母。 (请注意,带有kCFStringEncodingASCII
的CFStringGetCString
非常挑剔,会删除 ASCII 之外的字符;它不会为您提供字符串的 UTF8 表示形式。)名称包含非 ASCII 字符。因此,请务必学习CFString
和NSString
方法。请注意,CFStringRef
和NSString*
可以自由互换。There's no guarantee that the valid
ABRecordID
starts at1
and ends atABAddressBookGetPersonCount(addressBook)
. You can't use the for-loop usingABRecordID
.Instead, obtain the
CFArray
containing all the people usingABAddressBookCopyArrayOfAllPeople
and iterate on it.Another comment is that you shouldn't use C string; most of the things can be done using the API of
CFString
andNSString
, which supports Unicode out of the box. By getting the C string specifyingkCFStringEncodingASCII
, you're basically destroying letters like é or ü, ગુજરાતી or 案. (Note thatCFStringGetCString
withkCFStringEncodingASCII
is quite picky and removes characters not in the ASCII; it doesn't give you UTF8 representation of the string.) There're many people whose name contains non-ASCII characters. So, please do learnCFString
andNSString
methods. Note that aCFStringRef
and anNSString*
can be freely interchanged.