加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 如何为alamofire Post请求中的一个参数传递一个nil值

发布时间:2020-12-14 05:29:21 所属栏目:百科 来源:网络整理
导读:我想传递一个nil值,即可选参数值之一.并且它必须在Alamofire Post请求中继续使用nil值.如果你告诉我下一步该如何进行会有所帮助吗? let image: UIImage = UIImage() let imageData = UIImagePNGRepresentation(image) let base64String = imageData?.base64
我想传递一个nil值,即可选参数值之一.并且它必须在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)
    }

即使profile_pictures为空,响应也应该成功.我知道可以通过可选链接完成,但不知道如何继续!

通过传递nil或未初始化的可选参数Server将获得Optional

您可以将NSNull()传递给字典

试试这个,就像

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

swiftyjson也将null作为NSNull处理

这里也有很好的参考
null / nil in swift language

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读