ios - 使用 alamofire 在 swift 中创建字典

标签 ios swift alamofire

我尝试在 swift 中使用 alamofire 创建一个 Web 请求,请求对象应该是这样的

{
  "warranty": 0,
  "descriptions": "string",
  "product_name": "string",
  "purchase_date": "23/10/2016",
  "product_image": "string",
  "product_receipt": "string",
  "serial_number": "string",
  "barcode_image": "string",
  "serial_number_image": "string",
  "product": {
    "id": 1
  },
  "user": {
    "id": 12
  }
}

所以为了得到这个,我把我的代码写成这样

let parameters :[String:AnyObject] = [
            "warranty":product.warrenty,
            "descriptions":product.longDescription,
            "product_name":product.initialName,
            "purchase_date":product.purchaseDate,
            "serial_number":product.serialCode,
            "product": [
                "id":product.id
            ],
            "user": [
                "id":userDefaults.getCustomerId()
            ]
        ]

但是当我执行请求时,服务器似乎不接受这种格式,可能是我分配的方式

"product": {
        "id": 1
      },
      "user": {
        "id": 12
      }

不正确,这里有什么问题?有人可以指出我在这里所做的问题

最佳答案

我遇到了同样的问题,问题是,我缺少需要设置 .JSON 的编码选项。将下面的行与您的相匹配。

Alamofire.request(.POST, strURL, parameters: parameter as? [String : AnyObject], encoding: .JSON).responseJSON { (response: Response<AnyObject, NSError>) in

关于ios - 使用 alamofire 在 swift 中创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482795/

相关文章:

ios - Cordova/PhoneGap 3.1 iOS 空白页与远程 URL

ios - 如何触摸 UISlider 拇指阻止 UIScrollView 滚动

ios - 如何对齐嵌入在 UITextView 中的图像

ios - 当 func 中的归档对象编码时(使用 aCoder : NSCoder) method crashed with swift enum in real revice

Swift OneSignal OSPermissionSubscriptionState setSubscription

swift - 如何用Alamofire(帖子)上传图片?

ios - 网速慢或服务器宕机时如何处理Alamofire请求?

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

python - 安装 coremltools 时出错

swift - 使用 Alamofire swift 登录后如何向我的 UserProfileViewController 显示用户详细信息