在Xcode6 beta 1中,类似的东西起作用,但是在Xcode6 beta 4中有错误。修复错误后,我的无错误代码在
之下 背景:
我在这里使用的类实现ABPeoplePickerNavigationControllerDelegate
,允许用户从地址簿中选择联系人。我想收到第一封添加到该人的电子邮件。
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {
var emails = ABRecordCopyValue(person, kABPersonEmailProperty)
var emailsObj = emails.takeUnretainedValue() as AnyObject
var index = 0 as CFIndex
var emailAddress = ABMultiValueCopyValueAtIndex(emailsObj, index)
println(emailAddress)
}
然而,当我编译时,我得到Build Failed:
Bitcast要求两个操作数都是指针 %80 = bitcast%objc_object *%79到%PSs9AnyObject_,!dbg!860 存储值类型与指针操作数类型不匹配! 存储%PSs9AnyObject_%80,%objc_object **%81,对齐4,!dbg!860 %objc_object *存储值类型与指针操作数类型不匹配! store%PSs9AnyObject_%80,%objc_object **%85,align 4,!dbg!864 %objc_object * LLVM错误:发现功能损坏,编译中止! 命令/Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift失败,退出代码为1
可疑线是:
var emailsObj = emails.takeUnretainedValue() as AnyObject
var emailAddress = ABMultiValueCopyValueAtIndex(emailsObj, index)
当我将代码更改为
时func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {
var emails = ABRecordCopyValue(person, kABPersonEmailProperty)
var emailsObj:AnyObject? = emails.takeRetainedValue() as AnyObject
var index = 0 as CFIndex
var emailAddress = ABMultiValueCopyValueAtIndex(emailsObj, index)
println(emailAddress)
}
错误缩短为
Bitcast要求两个操作数都是指针 %79 = bitcast%objc_object *%78到%PSs9AnyObject_,!dbg!856 存储值类型与指针操作数类型不匹配! 存储%PSs9AnyObject_%79,%objc_object **%80,对齐8,!dbg!856 %objc_object * LLVM错误:发现功能损坏,编译中止! 命令/Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift失败,退出代码为1
提前感谢您提供任何帮助
亚伦
答案 0 :(得分:1)
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {
var unmanagedEmails = ABRecordCopyValue(person, kABPersonEmailProperty)
let emailObj: ABMultiValueRef = Unmanaged.fromOpaque(unmanagedEmails.toOpaque()).takeUnretainedValue() as NSObject as ABMultiValueRef
var index = 0 as CFIndex
var unmanagedEmail = ABMultiValueCopyValueAtIndex(emailObj, index)
var emailAddress:String = Unmanaged.fromOpaque(unmanagedEmail.toOpaque()).takeUnretainedValue() as NSObject as String
println(emailAddress)
}
可以找到更多完整答案here。