gpt4 book ai didi

swift - fatal error : unexpectedly found nil while unwrapping an Optional value with URLSession

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:55 26 4
gpt4 key购买 nike

我有一个在 Swift 2.2 中工作的方法,但是自从我将我的代码转换为 Swift 3 之后它就不再工作了,这个方法的作用是将用户名和密码登录到具有 Windows 身份验证的 URL,如果信用是正确则返回真,不正确则返回假。

方法如下:

func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
{
//Setup the NSURLSessionConfiguration

let configuration = URLSessionConfiguration.default

//Setup the NSURLSession

let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

//Add the username and password to NSURLCredential

credential = URLCredential(user:username, password:password, persistence: .forSession)

//Create request URL as String

let requestString = NSString(format:"%@", webservice) as String

//Convert URL string to NSURL

let url: URL! = URL(string: requestString)

//Prepare the task to get data.

let task = session.dataTask(with: url, completionHandler: {
data, response, error in

DispatchQueue.main.async(execute: {

if(error == nil)
{

//If there is no error calling the API, return true

completion(true)
}
else
{

//If there is an error calling the API, return false

completion(false)
}

})

})

//Run the task to get data.

task.resume()

}

我得到这个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

这发生在这里:

let task = session.dataTask(with: url, completionHandler: {
data, response, error in

DispatchQueue.main.async(execute: {

if(error == nil)
{

//If there is no error calling the API, return true

completion(true)
}
else
{

//If there is an error calling the API, return false

completion(false)
}

})

})

我做错了什么?

这出现在 fatal error 之前的调试导航器中:

function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A

我相信我的问题出在这里:

/**
Requests credentials from the delegate in response to a session-level authentication request from the remote server.
*/

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

if challenge.previousFailureCount > 0
{
completionHandler(Foundation.URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
}
else
{
completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust:challenge.protectionSpace.serverTrust!))
}

}

/**
Requests credentials from the delegate in response to an authentication request from the remote server.
*/

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential,credential)

}

它不喜欢这些方法。

最佳答案

更改 URL 声明自

let url: URL! = URL(string: requestString)

到:

let url: URL = URL(string: requestString)!

仅此一项就可以解决问题;否则它会显示您的 requestString 错误。

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

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