我尝试按照以下教程中的说明实现 NSURLProtocol:http://www.raywenderlich.com/76735/using-nsurlprotocol-swift
在 iOS8 中一切正常,但在 iOS7 中我在 startLoading() 中遇到运行时错误。
override func startLoading() {
var newRequest = self.request.copy() as NSMutableURLRequest //<- this line fails
NSURLProtocol.setProperty(true, forKey: "MyURLProtocolHandledKey", inRequest: newRequest)
self.connection = NSURLConnection(request: newRequest, delegate: self)
}
错误:WebCore:CFNetwork Loader(10):EXC_BREAKPOINT
有没有人成功实现过 NSURLProtocol?谢谢!
最佳答案
似乎在最新版本的 XCode (6.0.1) 中,无法将 NSURLRequest
转换为 NSMutableURLRequest
这是 swift 编译器的错误信息:
'NSURLRequest' is not convertible to 'NSMutableURLRequest'
您可以通过这种替代方式创建 NSMutableURLRequest
的实例
var newRequest = NSMutableURLRequest(URL: self.request.URL,
cachePolicy: self.request.cachePolicy,
timeoutInterval: self.request.timeoutInterval)
关于ios - NSURLProtocol & swift - ios7 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175807/