您的位置:首页 > 移动开发 > Swift

iOS: 学习笔记, Swift与C指针交互(译)

2014-07-31 18:17 357 查看


Swift与C指针交互

Objective-C和C API经常需要使用指针. 在设计上, Swift数据类型可以自然的与基于指针的Cocoa API一起工作, Swift自动处理几种常用的指针参数. 在本文中, 我们将看到C中的指针参数如何与Swift中的变量,数组,字符串一起工作.


指针作为输入/输出参数

C和Objective-C不支持多个返回值, 所以Cocoa API经常使用指针传递附加参数到函数. Swift允许把指针参数看成[inout]参数, 所以你可以用同样的&语法传递一个变量的引用作为指针. 例如: UIColor的getRed(_:green:blue:alpha:)方法使用4个CGFloat*指针来接受颜色的组合. 我们可以用&来得到这些值:

var r: CGFloat = 0, g:CGFloat = 0, b:CGFloat = 0, a:CGFloat = 0
color.getRed(&r, green: &g, blue: &b, alpha: &a)


另一个经常使用的是NSError. 许多方法使用了NSError*参数来保存发生的错误. 例如: 我们列举目录里的内容使用NSFileManager的contentsOfDirectoryAtPath(_:error:)方法, 直接使用NSError?变量来保存可能的错误:

var maybeError:NSError?
if let contents = NSFileManager.defaultManager().contentsOfDirectoryAtPath("/usr/bin", error: &maybeError){
//内容处理
for i in contents{
println(i)
}
}else if let error = maybeError{
//错误处理
println(error.description)
}


安全起见, Swift要求变量在使用&前需要初始化. 因为它不知道被调用的方法是否在修改它之前会读取指针


数组指针

在C中数组与指针紧紧相连. 为方便使用基于数组的C API, Swift允许将Array作为指针. 不可修改数组可以直接当成常量指针, 可修改数组可以使用&操作符做为非常量指针(就和inout参数一样). 例如: 我们把两个数组 a 和 b 使用vDSP_vadd函数(Accelerte framework)相加, 把结果写到第三个数组 result 中:

import Accelerate

let a: [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]



字符串指针

C使用 const char*指针作为传递字符串的主要方式. Swift String可以作为const char*指针, 它会给函数传递一个 null结束, UTF-8编码的字符串指针. 例如, 我们可以直接给标准C和POSIX库函数传递字符串:

puts("Hello from libc")
let fd = open("/tmp/scratch.txt", O_WRONLY|O_CREAT, 0o666)

if fd < 0 {
perror("could not open /tmp/scratch.txt")
} else {
let text = "Hello World"
write(fd, text, strlen(text))
close(fd)
}


指针参数转换的安全性

Swift尽可能让与C指针的交互方便并提供了一定的安全性, 因为C指针无处不在. 但是, 与C指针交互相对于Swift代码来说还是内存的不安全, 因此需要特别注意. 特别的:

* 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 could 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.

* Array和String指针没有边界检查. C API不会扩大数组和字符串, 因此在调用前你需要为它分配足够的大小

如果上面的指南不满足你指针交互的需要, 或者你想手动控制指针参数, 你可以直接使用unsafe指针操作内存. 我们会在将来的文章中看到更多高级应用.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift