我需要在 UITableView 中显示用户手机中可用的所有联系人列表。我可以成功检索 contactName 、 PhoneNumber 。但是当我将数据添加到 NSMutableArray 时,它会添加一些不寻常的字符。无法理解问题所在
-(void)viewDidLoad
{
Names=[[NSMutableArray alloc]init];
ABAddressBookRef allPeople = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts = ABAddressBookGetPersonCount(allPeople);
NSLog(@"numberOfContacts---%ld",numberOfContacts);
for(int i = 0; i < numberOfContacts; i++){
NSString* name = @"";
NSString* phone = @"";
NSString* email = @"";
ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);
ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);
NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
if (fnameProperty != nil) {
name = [NSString stringWithFormat:@"%@", fnameProperty];
}
if (lnameProperty != nil) {
name = [name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
}
if ([phoneArray count] > 0) {
if ([phoneArray count] > 1) {
for (int i = 0; i < [phoneArray count]; i++) {
phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
}
}else {
phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
}
}
if ([emailArray count] > 0) {
if ([emailArray count] > 1) {
for (int i = 0; i < [emailArray count]; i++) {
email = [email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
}
}else {
email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
}
}
NSLog(@"NAME : %@",name);
NSLog(@"PHONE: %@",phone);
NSLog(@"EMAIL: %@",email);
NSLog(@"\n");
NSMutableDictionary *d=[[NSMutableDictionary alloc]init];
[d setObject:name forKey:@"Name"];
[d setObject:phone forKey:@"Phone"];
[d setObject:email forKey:@"Email"];
[Names addObject:d];
}
NSLog(@"%@",Names);
这是名称的 NSLog :
{
Email = "";
Name = Hk;
Phone = "(975)\U00a0050-1890"; //Actual Number (975)050-1890
},
{
Email = "";
Name = Nik;
Phone = "1\U00a0(234)\U00a0567-890"; //Actual Number :(932)324-1343
}
为什么为电话号码添加不寻常的字符...... 任何帮助将不胜感激..
这是一个“不间断空格”(请参阅http://unicode-table.com/en/00A0/)。
这些符号似乎可以通过某种外部同步出现在您的地址簿中。 但我在视觉上没有任何区别。
如果您在 iPhone 通讯录中输入号码,它会自动格式化,括号和数字之间有真正的不可删除的空格。您的情况并非如此。你在视觉上根本没有空间。所以在代码中你有不间断的空格。