快速点击“发布方法” API时出现问题

时间:2019-05-28 13:07:59

标签: swift alamofire

我正在尝试调用一个API,在其中尝试发送一些参数和图像数组,但是当我点击API时,出现错误Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber dataUsingEncoding:]: unrecognized selector sent to instance 0xfb47bab3e1e91166'。我已经通过断点检查了它在参数上崩溃,我很困惑为什么会给出这个错误,我的API代码是这样,

func addAPI()
{
    let headers: HTTPHeaders = [
        /* "Authorization": "your_access_token",  in case you need authorization header */
        "Content-type": "multipart/form-data"
    ]


    let parameters :[String: Any]  = ["name":productNameTxt.text!,
                                      "price":priceTxt.text!,
                                      "size": sizeTxt.text!,
                                      "weight":weightTxt.text!,
                                      "quality":qualityTxt.text!,
                                      "brand":brandTxt.text!,
                                      "shippingCost":shippingCostTxt.text!,
                                      "details":detailTxt.text!,
                                      "material_id":materialId,
                                      "material_type_id": subMaterialId,
                                      "maxQuantity":4]


    Alamofire.upload(multipartFormData: { multipartFormData in

        for (key, value) in parameters {
            if let data = ((value) as AnyObject).data(using: String.Encoding.utf8.rawValue) {
                multipartFormData.append(data, withName: key)

            }
        }

        for (index, image) in self.imageArray.enumerated() {
            multipartFormData.append(image.pngData()!, withName: "file", fileName: "image\(index).png", mimeType: "image/png")
        }

//对于0中的i。

    },
                     to: addProductUrl,method:HTTPMethod.post,
                     headers:headers, encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload
                                .validate()
                                .responseJSON { response in
                                    switch response.result {
                                    case .success(let value):
                                      //  compBlock(value as AnyObject,true)
                                        print("responseObject: \(value)")
                                    case .failure(let responseError):
                                        print("responseError: \(responseError)")
                                    }
                            }
                        case .failure(let encodingError):
                            print("encodingError: \(encodingError)")
                            let errorDesc = (encodingError as NSError).localizedDescription
                            //failure(errorDesc as NSString,false)
                        }
    })

}

此错误是什么原因,我如何将其删除?

2 个答案:

答案 0 :(得分:1)

从这里

if let data = ((value) as AnyObject).data(using: String.Encoding.utf8.rawValue) {

那么所有传递的值都必须转换为 Data ,并且由于您指定了整数(包裹在json中时为NSNumber),因此当机,请更改 4 “ 4”

"material_id":"\(materialId)",

答案 1 :(得分:0)

如果您这样进行,那就更好了:

 //[String:String] instead of [String : Any]
 let parameters :[String: String]  = ["name":productNameTxt.text!,
                                  "price":priceTxt.text!,
                                  "size": sizeTxt.text!,
                                  "weight":weightTxt.text!,
                                  "quality":qualityTxt.text!,
                                  "brand":brandTxt.text!,
                                  "shippingCost":shippingCostTxt.text!,
                                  "details":detailTxt.text!,
                                  "material_id":"\(materialId)",
                                  "material_type_id": subMaterialId,
                                  "maxQuantity":4]


multipartFormData.append((value.data(using: .utf8))!, withName: key)