samdmarshall / SDMMobileDevice

MobileDevice Implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftTest Crash on Xcode 7

felix-dumit opened this issue · comments

I'm trying to run the swift example (Xcode 7), and after doing some minor changes to convert to Swift 2 syntax

import SDMMobileDevice

InitializeSDMMobileDevice();

func getDeviceAtIndex(array: NSArray, index: Int) -> SDMMD_AMDeviceRef {
    let device = array.objectAtIndex(index);
    return device as! SDMMD_AMDeviceRef;
}

var deviceList = SDMMD_AMDCreateDeviceList().takeRetainedValue();

var device = getDeviceAtIndex(deviceList, index: 0);

var result1: sdmmd_return_t = SDMMD_AMDeviceConnect(device);
print(result1);
var result2 = SDMMD_AMDeviceStartSession(device);
print(result2)
var domain = CFStringCreateWithCString(kCFAllocatorDefault, "NULL", 0);
var key = CFStringCreateWithCString(kCFAllocatorDefault, "UniqueDeviceID", 0);
var udid = SDMMD_AMDeviceCopyValue(device, domain, key);
//PrintCFType(udid);
var result3 = SDMMD_AMDeviceStopSession(device);
print(result3);
var result4 = SDMMD_AMDeviceDisconnect(device);
print(result4);
//SDMMD_AMDServiceConnectionGetDevice

I get a crash on return device as! SDMMD_AMDeviceRef; everytime. Because it cannot convert from AnyObject to COOpaquePointer (SDMMD_AMDeviceRef).

Do you know a possible solution?

Can you reproduce the same behavior when using the CFArray accessors instead of bridging the array over to an NSArray type object? My concern is that this won't handle well because the CF objects I create have no objc counterparts and creating custom CF objects using the CoreFoundation runtime is technically a "private" API.

A suggestion that was made was that you should try using an unsafeBitcast instead of a as! to do the type conversion (https://twitter.com/jckarter/status/700044050070548480). The downside to that is that you would have to do manual retain/release with the objects.

Do you mean like this:
var device = CFArrayGetValueAtIndex(deviceList, 0) as! SDMMD_AMDeviceRef ?
Compiles complains casting will always fail.

I tried with unsafeBitcast and it seems to work. I'll make a PR with the update