我有這個方法在Swift 2.2中工作,但自從我將代碼轉換爲Swift 3後,它不再有效,此方法所做的就是取用戶名和密碼登錄到Windows身份驗證的URL中,如果信譽正確,則返回true,如果它們不正確,則返回false。致命錯誤:意外地發現零,同時解開URLSession的可選值
這裏是方法:
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)
}
})
})
我在做什麼錯?
這出現在我的調試導航器中的致命錯誤之前:
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)
}
它不喜歡這些方法。
例如,如果它的工作,我會接受你的答案。 – user979331
'url'可能是零。 'requestString'的價值是什麼? – dan
url與requestString不是零,它不是零 – user979331