2

我正在慢慢地将我的代码库迁移到 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 转移,所以我需要它(现在)向后 -兼容的。

4

1 回答 1

1

使用 Xcode 7.0.1,我可以使您的代码工作,并进行一些更改。

首先,在Using Swift with Cocoa and Objective-C (Swift 2)指南(“自动释放指针”一章)中,它说声明为采用 an 的函数AutoreleasingUnsafeMutablePointer<Type>可以接受inout相同的表达式Type

因此,代码可以更改如下:

let info1: String?
let info2: String?
let info3: String?
let info4: String?
if !obj.extractInformation(&info1, info2: &info2, info3: &info3, info4: &info4, fromSource: "") {
    print("NO")
}else{
    print("YES")
}

但是,它仍然无法正常工作。

但是,在同一页面中,它说这AutoreleasingUnsafeMutablePointer<Type>适用于类型映射。我不知道这是否是它不起作用的原因,很难猜测背后发生了什么,但如果你替换StringNSString,你会得到一个结果。

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<NSString?>,
                            info2: AutoreleasingUnsafeMutablePointer<NSString?>,
                            info3: AutoreleasingUnsafeMutablePointer<NSString?>,
                            info4: AutoreleasingUnsafeMutablePointer<NSString?>,
                            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()

var info1: NSString?
var info2: NSString?
var info3: NSString?
var info4: NSString?
if !obj.extractInformation(&info1, info2: &info2, info3: &info3, info4: &info4, fromSource: "") {
    print("NO")
}else{
    print("YES")
    info1                // "ABC"
    info2                // "DEF"
    info3                // "GHIto be implemented"
    info4                // "to be implemented"
}
于 2015-10-08T13:47:26.457 回答