在Swift中为引用的C结构中的属性设置值
我想做的是修改我有引用的C结构的值,如下所示: 在bridgengheader.h中:在Swift中为引用的C结构中的属性设置值,swift,Swift,我想做的是修改我有引用的C结构的值,如下所示: 在bridgengheader.h中: struct info_type { int priority; }; 在ViewController.swift中: class MyClass { func viewDidLoad() { var info = info_type() info.priority = 2 processInfo(&info) }
struct info_type {
int priority;
};
在ViewController.swift中:
class MyClass {
func viewDidLoad() {
var info = info_type()
info.priority = 2
processInfo(&info)
}
func processInfo(infoRef: UnsafePointer<info_type>) {
info.memory.priority = 1
}
}
我是做错了什么,还是偶然发现了Xcode错误?我使用的是Xcode 7.0 beta 2(版本7.0 beta(7A121l)),因为
processInfo()
方法修改指向的内存
通过infoRef
,必须将参数声明为可变指针:
func processInfo(infoRef:UnsafeMutablePointer){
infoRef.memory.priority=1
}
这将按照预期进行编译和工作
Xcode 6.4为您的代码发出正确的错误消息:
func processInfo(infoRef: UnsafePointer<info_type>) {
infoRef.memory.priority = 1 // error: cannot assign to the result of this expression
}
func processInfo(infoRef:UnsafePointer){
infoRef.memory.priority=1//错误:无法分配到此表达式的结果
}
但是Xcode 7 beta 2崩溃,这是一个bug
func processInfo(infoRef: UnsafeMutablePointer<info_type>) {
infoRef.memory.priority = 1
}
func processInfo(infoRef: UnsafePointer<info_type>) {
infoRef.memory.priority = 1 // error: cannot assign to the result of this expression
}