iOS Swift登录错误:致命错误:在展开可选值时意外发现nil
我需要你的帮助,因为我不能使用这段代码。 我在obj-c中使用它,现在我想在iOS swift中使用它,但当我模拟它时,使用webData时出现错误,因为它为零,但我不知道如何修复它,因为它与我在obj-c中使用了很长时间的相同 这是我的代码:iOS Swift登录错误:致命错误:在展开可选值时意外发现nil,ios,swift,xcode6,nsmutabledata,Ios,Swift,Xcode6,Nsmutabledata,我需要你的帮助,因为我不能使用这段代码。 我在obj-c中使用它,现在我想在iOS swift中使用它,但当我模拟它时,使用webData时出现错误,因为它为零,但我不知道如何修复它,因为它与我在obj-c中使用了很长时间的相同 这是我的代码: func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!){ NSLog("webData:\(webData)")
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!){
NSLog("webData:\(webData)")
webData.length = 0
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
webData.appendData(data)
}
func connection(connection: NSURLConnection, didFailWithError error: NSError!){
NSLog("ERROR with theConenction")
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace!) -> Bool{
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge!){
if challenge.previousFailureCount >= 3{
textResult = "this connection requires user & password"
connection.cancel()
} else {
NSLog("didRecieveAuthentication")
var credential:NSURLCredential = NSURLCredential.credentialWithUser(txt_user.text, password: txt_pass.text, persistence: NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
NSLog("Credential: \(credential)")
}
}
func connectionDidFinishLoading(connection: NSURLConnection!){
NSLog("DONE. Recieved Bytes: \(webData.length)")
NSLog("webData\n\(webData)")
var convertToStringData: NSString = NSString(data: webData, encoding: NSUTF8StringEncoding)
textResult = convertToStringData
NSLog("textResult:\n\(textResult)")
}
func validateUserCredentials(){
var txtURL: NSString = "http://ws.itteria.cat/leina.nsf"
var nodeContent:NSMutableString
var locationOfWebService: NSURL = NSURL.URLWithString(txtURL)
NSLog("web url = \(locationOfWebService)")
var theRequest: NSMutableURLRequest = NSMutableURLRequest(URL: locationOfWebService)
theRequest.addValue("text/html", forHTTPHeaderField: "Content-Type")
theRequest.HTTPMethod = "GET"
var connect: NSURLConnection = NSURLConnection.connectionWithRequest(theRequest, delegate: self)!
}
我正在调用func ValidateCredentials,然后在使用webData时出现以下错误:
webData nil
fatal error: unexpectedly found nil while unwrapping an Optional value
webData在此声明:
class LoginViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate, NSURLSessionDelegate{
let webData: NSMutableData = NSMutableData(bytes: nil, length: 0)
希望你能帮助我,因为我不知道我还能做什么:S
谢谢这意味着
webData
已经被分配了一个nil
值。
因为您将其声明为隐式展开的可选var webData:NSMutableData代码>,您希望它始终被分配一个非零值
解决方案是将其设置为可选的var webData:NSMutableData?
,并在赋值后使用可选绑定检查它是否包含值或nil-most-likey
建议阅读:如果我使用var webData:NSMutableData?
那么我就不能使用webData.length=0
或webData.appendData(data)
--“NSMutableData”没有名为“length”的成员。请使用可选绑定(请参阅链接文档):如果让webData=webData{…}
。将您的代码添加到内部这并不能解决问题,但我已经使用以下方法解决了:)让webData:NSMutableData=NSMutableData(字节数:零,长度:0)
无论如何,谢谢;)隐马尔可夫模型。。。这让我觉得你的逻辑有问题。您能否在问题中发布声明、初始化和分配webData的代码?