我正在慢慢地将我的代码库迁移到 Swift 并遇到了一个奇怪的崩溃,我在其中填充了AutoreleasingUnsafeMutablePointer<String?>
一个非零字符串。这是一些按比例缩小的代码:
extension String {
func getRegexVariableNamed(name: String, forRegexString regexString: String) -> String? {
/** ... **/
return "TO BE IMPLEMENTED".lowercaseString // Using lowercase to prevent the compiler from inlining
}
}
class XYZ {
func extractInformation(info1: AutoreleasingUnsafeMutablePointer<String?>, info2: AutoreleasingUnsafeMutablePointer<String?>, info3: AutoreleasingUnsafeMutablePointer<String?>, info4: AutoreleasingUnsafeMutablePointer<String?>, fromSource source: String) -> Bool {
guard let vp = source.getRegexVariableNamed("ID", forRegexString: "vp=(?P<ID>\\d+)") else {
return false
}
info4.memory = vp
info1.memory = "ABC"
info2.memory = "DEF"
info3.memory = "GHI" + vp
return true
}
}
// Code in playground
let obj = XYZ()
let info1 = AutoreleasingUnsafeMutablePointer<String?>()
let info2 = AutoreleasingUnsafeMutablePointer<String?>()
let info3 = AutoreleasingUnsafeMutablePointer<String?>()
let info4 = AutoreleasingUnsafeMutablePointer<String?>()
if !obj.extractInformation(info1, info2: info2, info3: info3, info4: info4, fromSource: "") {
print("NO")
}else{
print("YES")
}
该应用程序(同样适用于游乐场)崩溃error: Playground execution aborted: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
将字符串设置为AutoreleasingUnsafeMutablePointer
方法中的时会发生此异常extractInformation(...)
。
在将此作为错误报告给 Apple 之前,我想检查一下我是否在这里做错了。
顺便说一句,我在 OS X 10.10.5 上使用最新的 Xcode 7 beta (6)。
PS:我知道更快速的方法是创建一个返回结构而不是使用这些自动释放指针的方法,但如前所述,我正在慢慢从 ObjC 转移,所以我需要它(现在)向后 -兼容的。