.. highlight:: java ================== Toll-Free Bridging ================== Here we will explain how you can use C-based and Objective-C based APIs side-by-side and pass objects between each other. More precisely we will show some examples on how to use Multi-OS Engine's Toll-Free bridging support. Important note here is C-based APIs have manual memory management. For better understanding on how it works, refer to Apple's `Ownership Policy documentation`_. For information about which types are bridgeable, visit `this`_ document. Objective-C to C ================ Sometimes there is a need to convert Foundation classes to CoreFoundation types. This can be done with the ``ObjCRuntime.cast(from, toType)`` method:: // Create an URL to intel.com and get its data NSURL url = NSURL.URLWithString("http://www.intel.com"); NSData ns = NSData.dataWithContentsOfURL(url); // Cast the NSData to a CFDataRef CFDataRef cf = ObjCRuntime.cast(ns, CFDataRef.class); // Retain the data CFRetain(cf); // Create a string from the data int enc = CFStringBuiltInEncodings.EncodingUTF8; CFStringRef string = CFStringCreateFromExternalRepresentation( kCFAllocatorDefault(), cf, enc); // Release the data CFRelease(cf); // Get the first 10 characters CharPtr buffer = PtrFactory.newCharArray(10); CFStringGetCharacters(string, new CFRange(0, 10), buffer); System.out.println("First 10 chars are '" + new String(buffer.toCharArray(10)) + "'"); // Release the string CFRelease(string); Output will probably be something like ``First 10 chars are '