使用swift访问iOS中的联系人详细信息

时间:2014-07-23 04:57:30

标签: ios nsstring swift abaddressbook

使用Objective-C时,我们通常使用以下代码来获取详细信息

NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

在Swift中,我尝试了以下

var firstName : NSString = ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty).takeUnretainedValue() as NSString

由于错误

,我无法构建
Bitcast requires both operands to be pointer or neither
  %224 = bitcast %objc_object* %223 to %PSs9AnyObject_, !dbg !486
Bitcast requires both operands to be pointer or neither
  %225 = bitcast %PSs9AnyObject_ %224 to i8*, !dbg !486
LLVM ERROR: Broken function found, compilation aborted!
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1

2 个答案:

答案 0 :(得分:12)

这适用于Xcode 6 GM

let firstName = ABRecordCopyValue(contactRecord, kABPersonFirstNameProperty).takeRetainedValue() as String

比上面的例子略显简洁。

答案 1 :(得分:3)

以下在Xcode6 beta 4中运行良好。

var firstName: NSString! = Unmanaged<CFString>.fromOpaque(ABRecordCopyValue(record, kABPersonFirstNameProperty).toOpaque()).takeUnretainedValue().__conversion()