how to pass a nil value for one of the parameter in alamofire Post request

做~自己de王妃 提交于 2019-12-01 15:19:04

问题


I would like to pass a nil value i.e., optional to one of the parameter value. And it must proceed with the nil value in the Alamofire Post request .It would be helpful if you tell me how to proceed next?

    let image: UIImage = UIImage()
    let imageData = UIImagePNGRepresentation(image)
    let base64String = imageData?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

    let parameters = [
        "first_name": "XXXXX",
        "email" : "1234@gmail.com",
        "password" : "password",
        "profile_picture" : base64String]

Alamofire.request(.POST, "http://abc/public/user/register", parameters: parameters, encoding: .JSON, headers: nil)

        .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
            print(totalBytesWritten)

            // This closure is NOT called on the main queue for performance
            // reasons. To update your ui, dispatch to the main queue.
            dispatch_async(dispatch_get_main_queue()) {
                print("Total bytes written on main queue: \(totalBytesWritten)")
           }
        }
        .responseJSON { response in
            debugPrint(response)
    }

The response should gets succeeded even if the profile_pictures is empty. I know it can be done with optional chaining but don't know how to proceed!!


回答1:


By passing nil or uninitialized optional parameter Server will get Optional

You can pass NSNull() to dictionary

try this, like

var params = ["paramA","valueA"] if imageBase64 == nil {   parms["image"] = NSNull()} else {   params["image"] = imageBase64 }

swiftyjson also handle null as NSNull

also there is good reference here null / nil in swift language




回答2:


I think your simplest answer would be to add "profile_picture" as a second step.

var parameters = [
    "first_name": "XXXXX",
    "email" : "1234@gmail.com",
    "password" : "password"]

if let base64String = base64String where !base64String.isEmpty {
    parameters["profile_picture"] = base64String
}



回答3:


hope this will help you :)

var parameters :[String : AnyObject?] = [
    "first_name": "XXXXX",
    "email" : "1234@gmail.com",
    "password" : "password"]

if let string = base64String where base64String.isEmpty != false {
  parameters["profile_picture"] = string 
} 



回答4:


After a very thorough research, I found out it can be done easily through optional chaining and type casting.

The first step it to divide the parameters by type casting it to string and Image and check for the availability of String value and Image value.

let parameters: [String : AnyObject? ] = [
    "first_name": "XXXXX",
    "email" : "1234@gmail.com",
    "password" : "password",
    "profile_pic" : UIImage(named: "logo")]

Do it like this in the request method

for (key, value) in parameters {

                if let value1 = value as? String {
                    multipartFormData.appendBodyPart(data: value1.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
                }
                if let value1 = value as? UIImage {
                  let imageData = UIImageJPEGRepresentation(value1, 1)
                      multipartFormData.appendBodyPart(data: imageData!, name: key, fileName: "logo.png" , mimeType: "image/png")
                    }

You don't have to split the parameters into two, one for the string values and other for the image and also it is unnecessary to convert the image to string. Hope this solution helps!!



来源:https://stackoverflow.com/questions/36716373/how-to-pass-a-nil-value-for-one-of-the-parameter-in-alamofire-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!