IOS: learning notes, Swift and C pointer Interaction)
Objective-C and C APIs for interaction between Swift and C pointers usually require pointers. in terms of design, Swift data types can naturally work with pointer-based Cocoa APIs. Swift automatically processes several common pointer parameters. in this article, we will see how pointer parameters in C work with variables, arrays, and strings in Swift. pointers as input/output parameters C and Objective-C do not support multiple return values. Therefore, Cocoa APIs often use pointers to pass additional parameters to functions. swift allows you to regard pointer parameters as [inout] parameters, so you can use the same & syntax to pass a variable reference as a pointer. for example, the getRed (_: green: blue: alpha :) method of UIColor uses four CGFloat * pointers to accept the combination of colors. we can use & to obtain these values: var r: CGFloat = 0, g: Float = 0, B: Float = 0, a: Float = 0color. get Red (& r, green: & g, blue: & B, alpha: & a) Another frequently used NSError. many methods use the NSError * parameter to save errors. for example, we use the contentsOfDirectoryAtPath (_: error :) method of NSFileManager to list the contents in the directory and directly use NSError? Variable to save possible errors: copy the code var maybeError: NSError? If let contents = NSFileManager. defaultManager (). contentsOfDirectoryAtPath ("/usr/bin", error: & maybeError) {// content processing for I in contents {println (I )}} else if let error = maybeError {// error handling println (error. description)} for the sake of code security, Swift requires variables to be initialized before use. because it does not know whether the called method will read the pointer array pointer before modifying it. In C, the array is closely linked to the pointer. to facilitate the use of Array-Based C APIs, Swift allows Array as a pointer. an array that cannot be modified can be directly regarded as a constant pointer. An array that can be modified can use the & operator as a constant pointer (just like the inout parameter ). for example, we add two arrays a and B using the vDSP_vadd function (Accelerte framework) and write the results to the third array result: copy the code import Accelerate let: [Float] = [1, 2, 3, 4] let B: [Float] = [0.5, 0.25, 0.125, 0.0625] var result: [Float] = [0, 0, 0, 0] vDSP_vadd (a, 1, B, 1, & result, 1, 4) // result now contains [1.5, 2.25, 3.125, 4.0625] copying the code string pointer C uses the const char * pointer as the main way to pass the string. swift String can be used as the const char * pointer, which will pass the function a null end, a String pointer encoded by the UTF-8. for example, we can directly pass a string to the Standard C and POSIX library functions: copy the code puts ("Hello from libc") let fd = open ("/tmp/scratch.txt ", o_WRONLY | O_CREAT, 0o666) if fd <0 {perror ("cocould not open/tmp/scratch.txt")} else {let text = "Hello World" write (fd, text, strlen (text) close (fd)} Security of copying code pointer parameter conversion Swift makes interaction between C pointers as convenient as possible and provides a certain degree of security, because C pointers are everywhere. however, the interaction with the C pointer is not safe than the Swift code, so pay special attention to it. special: * These conversions cannot safely be used if the callee saves the pointer value for use after it returns. the pointer that results from these conversions is only guaranteed to be valid for the duration of a call. even if you pass the same variable, array, or string as multiple pointer arguments, you cocould receive a different pointer each time. an exception to this is global or static stored variables. you can safely use the address of a global variable as a persistent unique pointer value, e.g.: as a KVO context parameter. * The Array and String pointers have no boundary check. c api does not expand the array and string, so you need to allocate enough size for it before calling