Skip to content

Instantly share code, notes, and snippets.

@ejknapp
Last active August 29, 2015 14:03
Show Gist options
  • Save ejknapp/2a827188ebd6aa3d1212 to your computer and use it in GitHub Desktop.
Save ejknapp/2a827188ebd6aa3d1212 to your computer and use it in GitHub Desktop.
//In C:
//==============
// The C Function we need to call:
//
// OSStatus someFunctionForData (
// int *outType,
// const void **outData
// )
// Will hold the type
UInt32 type = 0;
// The buffer for our important data
const void *data = NULL;
// Retrieve the data and its type
OSStatus status = someFunctionForData(&type, &data);
// Check for the desired type
if (type == kRealData_myType) {
// Cast the buffer to the struct for use
RealDataType *myData = (RealDataType *)data;
}
//In Swift:
//==============
// The Swift func we need to call
//
// func someFunctionForData(
// outType: UnsafePointer<Int>,
// outData: UnsafePointer<ConstUnsafePointer<()>>
// ) -> OSStatus
// Will hold the type
var type:UInt32 = 0
// The buffer for our important data.
// The ConstUnsafePointer is for data we don't know about ahead of time.
var data = ConstUnsafePointer<()>()
// The status var
var status = OSStatus(noErr)
// Retrieve the data and its type
status = someFunctionForData(&type, &data)
// Check for the desired type
if type == kRealData_myType {
// This is equivalent to the C cast above. It accesses the
// raw memory at the pointer address. The var does not need
// a specified type due to type inference, but can have one.
var myData = UnsafePointer<RealDataType>(data).memory
}
@ejknapp
Copy link
Author

ejknapp commented Jul 7, 2014

This no longer works in Xcode 6 beta 3. I'm working on the fix.

@ejknapp
Copy link
Author

ejknapp commented Jul 9, 2014

This gist has been updated for Xcode 6 beta 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment