我尝试按照以下教程中的说明实施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加载程序(10):EXC_BREAKPOINT
是否有人成功实现了NSURLProtocol?谢谢!
最佳答案
您的问题是,(非可变)NSURLRequest的副本是另一个非可变NSURLRequest,因此无法将其强制转换为NSMutableURLRequest。尝试:
var newRequest = self.request.mutableCopy() as NSMutableURLRequest // mutableCopy() instead of copy()
这应该为您提供了原始请求的可变副本,该副本应该很好。
关于ios - NSURLProtocol和Swift-iOS7中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175807/