Unmanaged issues in Swift Beta 5

The following Unmanaged call breaks in Swift Beta 5.

import Foundation

var obj = NSObject()
var unretainedRefToObject = Unmanaged.passUnretained(obj)

Apparently, the compiler is unhappy:

Stored value type does not match pointer operand type!
  store %CSo8NSObject* %0, %objc_object** %.targetPtr._value, align 8, !dbg !879
 %objc_object*LLVM ERROR: Broken function found, compilation aborted!
Command /Applications/Xcode6-Beta5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

Workaround (hack)

We’re using this workaround which stores a COpaquePointer:

import Foundation

var obj = NSObject()
var opaquePointerToUnretainedRefToObj = Unmanaged.passUnretained(obj).toOpaque()

Using the reference now requires one more step (and associated type bookkeeping):

var value = Unmanaged<NSObject>.fromOpaque(opaquePointerToUnretainedRefToObj).takeUnretainedValue()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s