Swift Compile Error Bitcast requires both operands to be pointer or neither
Question
In Xcode6 beta 1 something similar to this worked, however with Xcode6 beta 4 there were errors. After fixing the errors the errorless code I have is below
For Background:
The class I'm using here implements ABPeoplePickerNavigationControllerDelegate
which allows you the user to select a contact from the address book. I want to get the first email that is added to that person.
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)
}
However when I compile I get Build Failed:
Bitcast requires both operands to be pointer or neither %80 = bitcast %objc_object* %79 to %PSs9AnyObject_, !dbg !860 Stored value type does not match pointer operand type! store %PSs9AnyObject_ %80, %objc_object** %81, align 4, !dbg !860 %objc_object*Stored value type does not match pointer operand type! store %PSs9AnyObject_ %80, %objc_object** %85, align 4, !dbg !864 %objc_object*LLVM ERROR: Broken function found, compilation aborted! Command /Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1
The suspect lines are:
var emailsObj = emails.takeUnretainedValue() as AnyObject
var emailAddress = ABMultiValueCopyValueAtIndex(emailsObj, index)
When I change the code to
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)
}
The error shortens to
Bitcast requires both operands to be pointer or neither %79 = bitcast %objc_object* %78 to %PSs9AnyObject_, !dbg !856 Stored value type does not match pointer operand type! store %PSs9AnyObject_ %79, %objc_object** %80, align 8, !dbg !856 %objc_object*LLVM ERROR: Broken function found, compilation aborted! Command /Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1
Thanks in advance for any help
Aaron
Solution
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)
}
More full answer can be found here.