Swift:致命错误:在展开可选值时意外发现 nil
Posted
技术标签:
【中文标题】Swift:致命错误:在展开可选值时意外发现 nil【英文标题】:Swift : fatal error: unexpectedly found nil while unwrapping an Optional value 【发布时间】:2015-05-15 12:31:53 【问题描述】:我正在尝试访问 REST Web 服务,但它给出了这个 - 致命错误:在展开 Optional 值时意外发现 nil。
下面是代码 - 所以我有这个 RESTParser 类 -
protocol RESTParserDelegate
func getReceiveData(data:NSMutableData,sender:RESTParser)
class RESTParser: NSObject, NSURLConnectionDataDelegate
var receiveData: NSMutableData!
var requestConnection: NSURLConnection!
var delegate: RESTParserDelegate?
func receiveData(resData:NSMutableData)
receiveData = resData
func requestConnection(reqConn:NSURLConnection)
requestConnection = reqConn
func httpRequest(myRequest:NSMutableURLRequest)
self.requestConnection = NSURLConnection(request: myRequest, delegate: self)
// NSURLConnectionDataDelegate methods
func connection(connection: NSURLConnection, didReceiveData data: NSData)
self.receiveData?.appendData(data)
func connectionDidFinishLoading(connection: NSURLConnection)
self.delegate?.getReceiveData(receiveData, sender: self) // error @ this line
self.delegate = nil
self.receiveData = nil
self.requestConnection = nil
func connection(connection: NSURLConnection, didFailWithError error: NSError)
NSLog("Failed with error - %@ ",error.localizedDescription)
func parseData(data:NSMutableData)
Error is @ this line - self.delegate?.getReceiveData(receiveData, sender: self) in connectionDidFinishLoading 函数。
并从我的项目详细信息视图控制器中调用它 - 但是一旦调用 getReceiveData 方法,就会出现这个致命错误。
class ProjectDetails_tab: UIViewController, RESTParserDelegate
override func viewDidLoad()
super.viewDidLoad();
//var projectData: NSDictionary = [String:String]()
var url:String = "http://domianName.com"
var nsURL = NSURL(string: url)
var createrequest: NSMutableURLRequest = NSMutableURLRequest(URL: nsURL!)
createrequest.HTTPMethod = "GET"
var rest = RESTParser()
rest.delegate=self
rest.httpRequest(createrequest)
func getReceiveData(data:NSMutableData,sender:RESTParser)
【问题讨论】:
在哪一行出现此错误...放置一些断点并检查... Error is @ this line - self.delegate?.getReceiveData(receiveData, sender: self) in connectionDidFinishLoading 函数。 【参考方案1】:不清楚代码的哪一行出现了致命错误。但是您可以重新检查您的委托对象,或者:
// NSURLConnectionDataDelegate methods
func connection(didReceiveResponse: NSURLConnection, didReceiveResponse response: NSURLResponse)
// Recieved a new request, clear out the data object
self.receiveData = NSMutableData()
func connection(connection: NSURLConnection, didReceiveData data: NSData)
self.receiveData?.appendData(data)
func connectionDidFinishLoading(connection: NSURLConnection!)
// Request complete, self.data should now hold the resulting info
【讨论】:
Error is @ this line - self.delegate?.getReceiveData(receiveData, sender: self) in connectionDidFinishLoading 函数。并且委托方法定义没有!,仍然尝试但 dinn 工作 这是因为 'receiveData' 对象为零。您必须在消息“connection(didReceiveResponse: NSURLConnection, didReceiveResponse response: NSURLResponse)”中初始化该对象在我编辑的答案中查看第一个函数以上是关于Swift:致命错误:在展开可选值时意外发现 nil的主要内容,如果未能解决你的问题,请参考以下文章
swift 2 致命错误:在展开可选值时意外发现 nil - 类别名称
Swift 和 UILabel - 致命错误:在展开可选值时意外发现 nil
(Swift) PrepareForSegue: 致命错误: 在展开可选值时意外发现 nil