本文介绍了如何修复“错误域= NSCocoaErrorDomain代码= 3840"无值." UserInfo = {NSDebugDescription =无值.}"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行代码时出现此错误,我也不知道为什么.

When i run my code i get this error and i don't know why.

错误域= NSCocoaErrorDomain代码= 3840无值." UserInfo = {NSDebugDescription =无值.}

Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

我在互联网上寻找它,但没有找到任何东西.

I looked for it on the internet but i didn't find something.

这是我的代码:

 let myUrl = NSURL(string: "http://foodhelper.club/registerUser.php");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let postString = "userEmail=\(userEmail!)&userFirstName=\(userFirstName!)&userLastName=\(userLastName!)&userPassword=\(userPassword!)";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        dispatch_async(dispatch_get_main_queue())
        {



            if error != nil {
                self.alertMessage(error!.localizedDescription)
                print("fail")
                return
            }

            do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
                print ("1")
              if let parseJSON = json {

                    let userId = parseJSON["userId"] as? String
                    print ("2")
                    if( userId != nil)
                    {
                        let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.Alert);

                        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(action) in

                            self.navigationController?.popViewControllerAnimated(true)                            }

                        myAlert.addAction(okAction);
                        self.presentViewController(myAlert, animated: true, completion: nil)
                    } else {
                        let errorMessage = parseJSON["message"] as? String
                        print ("3")
                        if(errorMessage != nil)
                        {
                            self.alertMessage(errorMessage!)
                        }

                    }

                }
            } catch{

                //email vergleich fehlt, egal ob
                print(error)
                print("catched error")

                let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.Alert);

                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(action) in


                    self.navigationController?.popViewControllerAnimated(true)
                }

                myAlert.addAction(okAction);
                self.presentViewController(myAlert, animated: true, completion: nil)


            }



        }

    }).resume()


}

谢谢您的帮助

推荐答案

您需要设置内容类型标头值以使用JSON.

You need to set the content-type header value to use JSON.

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

将代码更新为Swift 3,并删除了与请求无关的所有内容:

Updated the code to Swift 3 and removed everything unrelated to the request:

let myUrl = URL(string: "http://foodhelper.club/registerUser.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let postString = "userEmail=email&userFirstName=firstname&userLastName=lastname&userPassword=password";
request.httpBody = postString.data(using: String.Encoding.utf8);

URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in
        if error != nil {
            print("fail")
            return
        }

        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            print ("1")

            if let parseJSON = json {

                let userId = parseJSON["userId"] as? String
                print ("2")
                if( userId != nil) {

                } else {
                    let errorMessage = parseJSON["message"] as? String
                    print ("3")
                }
            }
        } catch{
            print(error)
        }
}).resume()

这篇关于如何修复“错误域= NSCocoaErrorDomain代码= 3840"无值." UserInfo = {NSDebugDescription =无值.}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:19