6

我知道NSString并且CFString是收费桥,他们可以以这种方式转换。

CFStringRef cfStr;
NSString* anStr=(NSString*)cfStr;

但是,当我在处理通讯录时在这种情况下转换字符串时

NSString* homeLabel=(NSString*)kABWorkLabel;

然后 homeLabel 是"_$!<Work>!$_";其他在地址簿中预定义CFStringRef的属性也是一样的。

在我的程序中,我想同时获取标签和标签项的信息。

NSString* label=ABMultiValueCopyLabelAtIndex(multi,i);

那么如何正确获取标签的字符串呢?

4

4 回答 4

22

尝试使用ABAddressBookCopyLocalizedLabel吗?

就像是:

  ABAddressBookRef ab = ABAddressBookCreate();
  ABRecordID personID = <someid>;
  CFIndex phoneNumberIndex = <anIndexFromSomewhere>;
  ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab, personID);

  CFStringRef name = ABRecordCopyCompositeName(person);
  ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
  CFStringRef number = ABMultiValueCopyValueAtIndex(phoneNumbers, phoneNumberIndex);
  CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, phoneNumberIndex);
  CFStringRef localizedLabel = ABAddressBookCopyLocalizedLabel(label);

  NSLog(@"Person: %@", name);
  NSLog(@"%@ : %@", localizedLabel, number);

  CFRelease(label);
  CFRelease(localizedLabel);
  CFRelease(number);
  CFRelease(phoneNumbers);
  CFRelease(name);
  CFRelease(ab);
于 2010-08-11T12:39:53.187 回答
6

尝试这个:

+ (NSString *) displayPropertyName:(NSString *) propConst{
    if ([propConst isEqualToString:@"_$!<Anniversary>!$_"]) return @"anniversary";
    if ([propConst isEqualToString:@"_$!<Assistant>!$_"]) return @"assistant";
    if ([propConst isEqualToString:@"_$!<AssistantPhone>!$_"]) return @"assistant";
    if ([propConst isEqualToString:@"_$!<Brother>!$_"]) return @"brother";
    if ([propConst isEqualToString:@"_$!<Car>!$_"]) return @"car";
    if ([propConst isEqualToString:@"_$!<Child>!$_"]) return @"child";
    if ([propConst isEqualToString:@"_$!<CompanyMain>!$_"]) return @"company main";
    if ([propConst isEqualToString:@"_$!<Father>!$_"]) return @"father";
    if ([propConst isEqualToString:@"_$!<Friend>!$_"]) return @"friend";
    if ([propConst isEqualToString:@"_$!<Home>!$_"]) return @"home";
    if ([propConst isEqualToString:@"_$!<HomeFAX>!$_"]) return @"home fax";
    if ([propConst isEqualToString:@"_$!<HomePage>!$_"]) return @"home page";
    if ([propConst isEqualToString:@"_$!<Main>!$_"]) return @"main";
    if ([propConst isEqualToString:@"_$!<Manager>!$_"]) return @"manager";
    if ([propConst isEqualToString:@"_$!<Mobile>!$_"]) return @"mobile";
    if ([propConst isEqualToString:@"_$!<Mother>!$_"]) return @"mother";
    if ([propConst isEqualToString:@"_$!<Other>!$_"]) return @"other";
    if ([propConst isEqualToString:@"_$!<Pager>!$_"]) return @"pager";
    if ([propConst isEqualToString:@"_$!<Parent>!$_"]) return @"parent";
    if ([propConst isEqualToString:@"_$!<Partner>!$_"]) return @"partner";
    if ([propConst isEqualToString:@"_$!<Radio>!$_"]) return @"radio";
    if ([propConst isEqualToString:@"_$!<Sister>!$_"]) return @"sister";
    if ([propConst isEqualToString:@"_$!<Spouse>!$_"]) return @"spouse";
    if ([propConst isEqualToString:@"_$!<Work>!$_"]) return @"work";
    if ([propConst isEqualToString:@"_$!<WorkFAX>!$_"]) return @"work fax";
    return @"";
}
于 2010-04-03T14:07:42.950 回答
0

您必须检测带有后缀和前缀的标签。然后做一个子字符串来获取标签。

您获得的值是地址簿数据库中字符串的正确标签。在呈现给用户之前,它们只是稍微修饰了一下。就这样。

于 2010-04-03T12:52:46.410 回答
0

在较新的联系人框架中,您有一个类函数

这是swift 4中的示例

let emailValue: CNLabeledValue<NSString> = ...
let label = emailValue.label ?? ""
let prettyLabel = type(of: emailValue).localizedString(forLabel: label)

"_$!<Work>!$_"变为"work"

更好的是,您可以进行扩展

extension CNLabeledValue {
  @objc func prettyLabel() -> String? {
    if let label = label {
      return  type(of: self).localizedString(forLabel: label)
    }
    return nil
  }
}

现在你有更简单的调用

let emailValue: CNLabeledValue<NSString> = ...
let prettyLabel = emailValue.prettyLabel()
于 2019-02-25T11:14:12.547 回答