我想做的是修改我引用的 C 结构的值,如下所示:
在 BridgingHeader.h 中:
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 中触发“命令因信号失败:Abort trap: 6”。打开我看到的构建输出
Assertion failed: (GetSetInfo.getInt().hasValue()), function getSetterAccessibility, file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.42.3/src/swift/include/swift/AST/Decl.h, line 4070.
0 swift 0x0000000106d17b9b llvm::sys::PrintStackTrace(__sFILE*) + 43
1 swift 0x0000000106d182db SignalHandler(int) + 379
2 libsystem_platform.dylib 0x00007fff8eaacf1a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fff5aee4bec _sigtramp + 3426974956
4 libsystem_c.dylib 0x00007fff8ef73b53 abort + 129
[...]
我是做错了什么还是我偶然发现了 Xcode bug?我正在使用 Xcode 7.0 beta 2(版本 7.0 beta (7A121l))
最佳答案
由于 processInfo()
方法修改了指向的内存
根据 infoRef
,参数必须声明为可变指针:
func processInfo(infoRef: UnsafeMutablePointer<info_type>) {
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
}
但是 Xcode 7 beta 2 崩溃了,这是一个错误。
关于swift - 在 Swift 中为引用的 C 结构中的属性设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249496/