0

我正在尝试使用 JSONEncoder 将四个不同的数组编码为 json 格式,但我无法转换数组。所以,请帮帮我。我正在为 iOS 11 使用 swift 4、xcode 9。

我的数组是:

var timeCart //dynamic data as string
var nameCart //dynamic data as string, ex. ["myNameOne", "myNameTwo", ...]
var quantityCart //dynamic data as Int, ex. [2, 3, 5, ...]
var priceCart //dynamic data as Int, ex. [82, 73, 65, ...]

我的结构是:

struct OrderDetailJSON: Encodable {
    let allOrder: [AllOrder]
    let TaxDetials: TaxDetail

    struct AllOrder: Encodable {
        let date_time: String
        let item: String
        let name: String
        let price: String
    }

    struct TaxDetail: Encodable {
        let tax_per: String
        let tax_name: String
    }
}

我正在尝试在表格视图中像这样对其进行编码

let orderDetailsJSON = OrderDetailJSON(allOrder: [.init(date_time: "\(quantityCart[indexPath.row])", item: "\(quantityCart[indexPath.row])", name: "\(nameCart[indexPath.row])", price: "\(priceCart[indexPath.row])")], TaxDetials: .init(tax_per: defaults.string(forKey: "STORE_TAX")!, tax_name: defaults.string(forKey: "STORE_TAX_NAME")!))
        do {
            let jsonData = try JSONEncoder().encode(orderDetailsJSON)
            let jsonString = String(data: jsonData, encoding: .utf8)
            print("order_details :" + jsonString!)
        }catch{

        }

我得到的输出是:

order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "Manchurian",
      "item": "2",
      "price": "87"
    }
  ]
}order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "3",
      "name": "Paneer Tikka",
      "item": "3",
      "price": "150"
    }
  ]
}order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "snacks",
      "item": "2",
      "price": "54"
    }
  ]
}

我期待的输出是:

order_details: {
  "TaxDetials": {
    "tax_per": "5",
    "tax_name": "CGST & SGST"
  },
  "allOrder": [
    {
      "date_time": "2",
      "name": "Manchurian",
      "item": "2",
      "price": "87"
    },
    {
      "date_time": "3",
      "name": "Paneer Tikka",
      "item": "3",
      "price": "150"
    },
    {
      "date_time": "2",
      "name": "snacks",
      "item": "2",
      "price": "54"
    }
  ]
}
4

1 回答 1

0

您的结构声明没有任何问题(除了您应该遵守 Swift 命名约定,即变量名称的 lowerCamelCase -taxPer而不是tax_per等,并且您的属性名称不使用正确的复数/单数形式) .

问题是您正在OrderDetailJSON为每个表格视图单元格定义一个实例,而您似乎TaxDetails应该对表格中的所有单元格都相同,并且您实际上想要创建一个OrderDetailJSON实例,其allOrder数组属性包含AllOrder每个表格视图单元格的一个实例. 您需要将allOrder属性更改为 mutable 才能正常工作。

class YourTableViewController: UITableViewController {
    var orderDetails: OrderDetailJSON
    ...
    // You might need to use a different tableView function depending on your exact needs
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        orderDetails.allOrder[indexPath.row] = AllOrder(date_time: "\(quantityCart[indexPath.row])", item: "\(quantityCart[indexPath.row])", name: "\(nameCart[indexPath.row])", price: "\(priceCart[indexPath.row])")
    }
}

您需要确保orderDetails使用正确的初始化,TaxDetails并且将相同数量的元素预分配给allOrder您将拥有的尽可能多的表格视图单元格。只需在orderDetails所有单元格都填满信息后将其转换为 JSON,您无需逐个单元格地执行此操作。

于 2018-05-25T12:02:38.167 回答