使用Alamofire中的参数上传图像时,无法识别的选择器发送到实例

时间:2016-04-18 15:24:26

标签: ios swift multipartform-data alamofire

我正在尝试使用Alamofire上传带参数的图片。图像已设置且不是零。当我尝试使用多部分表单数据附加正文部分时,我得到无法识别的选择器,如下所示。我在这做错了什么?使用Swift 2.2。任何帮助将不胜感激。

let parameters: [String : AnyObject] = ["f_name": keychain.get(userNameKeychain)!, "l_name": "", "email": "d.tha@aexonic.com", "password": keychain.get(passwordKeychain)!,"image": self.imgProfilePic.image!, "cover_image": self.imgBannerImage.image!, "country": keychain.get(countryKeychain)!, "street_address_1": keychain.get(streetAddress1Keychain)!, "street_address_2": keychain.get(streetAddress2Keychain)!, "brand_name": self.strBrandName, "town_city": keychain.get(cityKeychain)!, "state": keychain.get(stateKeychain)!, "zip": keychain.get(zipKeychain)!, "dob": timeStamp!, "latitude": self.latitude!, "longitude": self.longitude!, "is_verify": keychain.getBool(phoneNumberVerifyKeychain)!, "phone": keychain.get(phoneNumberKeychain)!];

        print(url_register);
        print("Parameters to post: \(parameters)");


         //"cover_image": ""
         //"image": ""
         // ABOVE TWO PARAMETERS NEED TO BE THERE IN parameters TO UPLOAD 2 IMAGES
         Alamofire.upload(.POST, url_register, headers: [authorization: authHeader], multipartFormData: { multipartFormData in

                // import image to request
                if self.imgProfilePic.image != nil {
                    if let imageData = UIImageJPEGRepresentation(self.imgProfilePic.image!, 1) {
                        multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "profile.png", mimeType: "image/png")
                    }

                    // import parameters
                    for (key, value) in parameters {
                        multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) //*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean dataUsingEncoding:]: unrecognized selector sent to instance 0x380bf6cc'
                    }
                }

                if self.imgBannerImage.image != nil {
                    if let imageData = UIImageJPEGRepresentation(self.imgBannerImage.image!, 1) {
                        multipartFormData.appendBodyPart(data: imageData, name: "cover_image", fileName: "cover.png", mimeType: "image/png")
                    }

                    // import parameters
                    for (key, value) in parameters {
                        multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
                    }
                }

            }, // you can customise Threshold if you wish. This is the alamofire's default value
            encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON { response in
                        debugPrint(response)
                    }
                case .Failure(let encodingError):
                    print(encodingError)
                }
                stopHUD(self.view);
        })

1 个答案:

答案 0 :(得分:3)

显然,来自Arrays.deepEquals的{​​{1}}中的一个不能收到消息value。这只适用于字符串。您可能希望在parameters循环中打印每个dataUsingEncoding()以调试导致问题的value