Swift函数的return语句产生一个“ SIGILL”

时间:2019-06-04 21:05:18

标签: swift linux callback urlsession

我正在用Swift编写一段代码来访问公共API终结点,以拉回JSON中的数据并在应用程序中使用它。我正在使用URLSession来执行请求,并且正在使用async / await类似的范例从URLSession回调中提取数据并将其放置在局部变量中。然后,该函数将JSONSerialization返回的可选字典返回给调用方。

此代码在函数外部可以很好地执行,并且可以作为主程序的一部分运行,但是一旦将其移至函数,则return语句将生成“ SIGILL”出口。

我在return语句上断点了,发现它正是引发此错误的原因。由于这是一个可选的字典,因此我尝试仅返回字典的未包装版本并找到相同的结果。我也尝试过只返回空白字典,但仍然收到SIGILL

功能:

let url = URL(string: <endpointURL>)!
var tenant: [String: Any]? = nil;
let sem = DispatchSemaphore(value: 1)
sem.wait()
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
    if let data = data {
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            if let json = json {
                print(json)
                tenant = json
                sem.signal()
            } else {
                print("ERR: Null JSON")
            }
        } catch let error as NSError {
            print("ERR: " + error.localizedDescription)
        }
    } else if let error = error {
        print("ERR: " + error.localizedDescription);
    } else {
        print("ERR: Unknown")
    }
}
print("resuming")
task.resume()
print("waiting: ")
sem.wait()
print("done waiting")

print(tenant!["tenant_name"]!)

失败:

let _ = HttpHelper.getTenantFor(tenantId: <someUUID>)
class HttpHelper {
    static func getTenantFor(tenantId: String) -> [String:Any]? {
        let url = URL(string: <endpointURL>)!
        var tenant: [String: Any]? = nil;
        let sem = DispatchSemaphore(value: 1)
        sem.wait()
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                    if let json = json {
                        print(json)
                        tenant = json
                        sem.signal()
                    } else {
                        print("ERR: Null JSON")
                    }
                } catch let error as NSError {
                    print("ERR: " + error.localizedDescription)
                }
            } else if let error = error {
                print("ERR: " + error.localizedDescription);
            } else {
                print("ERR: Unknown")
            }
        }
        print("resuming")
        task.resume()
        print("waiting: ")
        sem.wait()
        print("done waiting")
        return [String:Any]()
    }
}

在正常运行的代码上,应用程序为JSON对象中的“ tenant_name”键输出正确的值,在失败的代码中,我得到以下信息:

Process finished with exit code 132 (interrupted by signal 4: SIGILL)

0 个答案:

没有答案