gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value on sendSynchronousRequest

转载 作者:行者123 更新时间:2023-11-28 09:20:52 25 4
gpt4 key购买 nike

发送 NSURLRequest 时:

var request = NSMutableURLRequest(URL: url)
var err: AutoreleasingUnsafePointer<NSError?> = nil
var response: AutoreleasingUnsafePointer<NSURLResponse?> = nil
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: err) as NSData

在最后一个字符串上我得到了错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

此错误仅出现在 IOS 8 模拟器上。使用 IOS 7.1 模拟器,应用程序可以正常运行,没有任何错误。在发送请求之前,我尝试使用 println(request) 和 IOS 7.1 注销请求,我得到了:

<NSMutableURLRequest: 0x7b787130> { URL: http://www.example.com/api/search?name=test&page=1 }

使用 IOS 8:

<NSMutableURLRequest: 0x7b6246d0> { URL: http://www.example.com/api/search?name=test&page=1, headers: (null) }

然后我假设问题出在请求头上,所以我设置了一个:

request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")

但不幸的是它没有帮助,该应用程序仍然只能在 IOS 7.1 模拟器上运行,但不能在 IOS 8 上运行。请告诉我我做错了什么?

顺便说一句,如果有帮助的话,我正在使用 Xcode 6 beta 3。

最佳答案

我的应用程序出现了同样的错误,这就是我将代码更改为的内容,之后一切正常。请注意,在 sendSynchronousRequest 的末尾,'as NSData' 被删除了。

var dataVal =  NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error:nil)

if ( dataVal == nil ) {
// Handle error here
}

// Everything is fine! Rest of the code

希望这对您有所帮助。

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value on sendSynchronousRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com