简体   繁体   English

如何为alamofire Post请求中的参数之一传递零值

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

I would like to pass a nil value ie, optional to one of the parameter value. 我想传递一个nil值,即参数值之一的可选值。 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? 并且必须在Alamofire Post请求中继续使用nil值。如果您告诉我接下来如何进行操作会很有帮助?

    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. 即使profile_pictures为空,响应也应该成功。 I know it can be done with optional chaining but don't know how to proceed!! 我知道可以通过可选的链接来完成,但是不知道如何进行!

By passing nil or uninitialized optional parameter Server will get Optional 通过传递nil或未初始化的可选参数,Server将获得Optional

You can pass NSNull() to dictionary 您可以将NSNull()传递给字典

try this, like 试试这个,就像

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

swiftyjson also handle null as NSNull swiftyjson还以NSNull处理null

also there is good reference here null / nil in swift language 在这里也有很好的参考null / nil快速语言

I think your simplest answer would be to add "profile_picture" as a second step. 我认为您最简单的答案是添加"profile_picture"作为第二步。

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

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

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 
} 

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!! 希望此解决方案有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM