首页 > 解决方案 > NSCocoaErrorDomain 代码=3840 "没有值。"

问题描述

我正在尝试将一些数据发布到服务器并获得响应..像这样:

     @IBAction func submitbtn(_ sender: Any) {


    let decoded4  = userDefaults.object(forKey: "nationalities") as! Data
    let decodedNationalities = NSKeyedUnarchiver.unarchiveObject(with: decoded4) as! [Nationality]
    for nationality in decodedNationalities {
        if nationality.name == idnationality{
            idnationality = String(nationality.id)
        }

    }
    if conttype == "Single visit"{
        conttype = "single_visit"
    }else {
        conttype = "multi_visit"
    }
    print(days)
    if days.hasPrefix(","){
        days.remove(at: days.startIndex)
    }
    if days.hasSuffix(","){
        days.remove(at: days.endIndex)
    }
    let todosEndpoint: String = "my link"
    guard let todosURL = URL(string: todosEndpoint) else {
        print("Error: cannot create URL")
        return
    }
    var todosUrlRequest = URLRequest(url: todosURL)
    todosUrlRequest.httpMethod = "POST"
    todosUrlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let newTodo: [String: Any] = ["email": UserDefaults.standard.string(forKey: "CustomerEmail")!, "password": UserDefaults.standard.string(forKey: "CustomerPassword")!, "id_address": addid, "quantity_staff": quantitiy, "num_of_visits_per_week": numvisits, "id_service": idservice, "id_region": idregion, "id_city": idcity, "id_nationality": idnationality, "start_date": date, "contract_period": mon, "contract_type": conttype, "shift_type": shif, "weekdays": days]
    print(newTodo)
    let jsonTodo: Data
    do {
        jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
        todosUrlRequest.httpBody = jsonTodo
    } catch {
        print("Error: cannot create JSON from todo")
        return
    }

    let session = URLSession.shared

    let task = session.dataTask(with: todosUrlRequest) {
        (data, response, error) in
        guard error == nil else {
            print("error calling POST on /public/api/register_customer")
            print(error!)
            return
        }
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }

        // parse the result as JSON, since that's what the API provides
        do {
            guard let receivedTodo = try JSONSerialization.jsonObject(with: responseData,options: []) as? [String: Any] else {
                 print("Could not get JSON from responseData as dictionary")
            return
            }
            print("The todo is: " + receivedTodo.description)

            guard let status = receivedTodo["success"] as? Int else {
                print("Could not get status from JSON")
                return
            }

            if status == 0{
                DispatchQueue.main.async {
                     self.performSegue(withIdentifier: "segueerr", sender: self)
                }

                print("The status is: 0")
                guard let messages = receivedTodo["message"] as? String else {
                    print("Could not get messages from JSON")
                    return
                }
                    print(messages)


            }
            else {
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "successsegue", sender: self)
                }
                print("Success!")
            }
        } catch  {
            print(error)
            return
        }
    }
    task.resume()


}

但我不断收到此错误:

错误域 = NSCocoaErrorDomain 代码 = 3840 “无值。” UserInfo={NSDebugDescription=无值。}

当一切都按照我的意愿正确发送时:

 ["contract_period": 0, "email": "lamatat@gmail.com", "id_service": 1, "id_region": 1, "id_city": 3, "id_address": 22, "weekdays": "sun", "contract_type": "single_visit", "num_of_visits_per_week": "1", "id_nationality": "4", "password": "4169faf51ce3c5fb8850451b441a363906f16d69", "shift_type": "day", "quantity_staff": "1", "start_date": "2018-05-06"]

这也工作得很好,突然变成这样并显示此错误!

我已经从邮递员那里尝试了完全相同的值,我得到了正确的共鸣!

为什么我会收到这个错误?我做错了什么?

邮递员截图:

在此处输入图像描述

结果:

在此处输入图像描述

标签: iosswiftxcode

解决方案


推荐阅读