将数组数据模型转换为字典(swift)

时间:2016-02-05 07:40:26

标签: ios arrays swift dictionary datamodel

数据模型

class DataCart {
   var icon: UIImage?
   var cartId: String
   var price: Int
   var productName: String
   var quantity: Int


init(icon: UIImage?, cartId: String, price: Int, productName: String, quantity: Int){
    self.icon = icon
    self.cartId = cartId
    self.price = price
    self.productName = productName
    self.quantity = quantity

  }
}

词典

var cart = [DataCart]()

"products": [[
    "productName": "Photobook", //"dataCart.productName"
    "quantity": 2,   //"dataCart.quantity"
    "price": "5000.00",  //"dataCart.price"
    "pages": 40
    ],[
    "productName": "Photobook2",
    "quantity": 5,
    "price": "7000.00",
    "pages": 30
    ]]

for dataCart in cart {
........
}

我是数据模型的数组,我想把它显示为字典,我很困惑改变它。如何将cart的数组(DataCart)转换为字典?

2 个答案:

答案 0 :(得分:3)

在班级进行转换是一种很好的做法。

因此,将来如果你有更多的密钥,你只需要在课程级别进行更改,而不是在所有使用过相同位置的地方进行更改。

在课程中添加一个计算属性dictionayDataCart,如下所示。 还要添加一个方法“convertDataCartArrayToProductsDictionary:”,它将您的对象数组转换为字典。

 class DataCart {
        var icon: UIImage?
        var cartId: String
        var price: Int
        var productName: String
        var quantity: Int


        init(icon: UIImage?, cartId: String, price: Int, productName: String, quantity: Int){
            self.icon = icon
            self.cartId = cartId
            self.price = price
            self.productName = productName
            self.quantity = quantity

        }

        var dictionaryDataCart : [String : AnyObject] {

            var objDict : [String : AnyObject]!

            if let icon = self.icon {
                objDict["icon"] = icon;
            }

            objDict["cartId"] = self.cartId;
            objDict["price"] = price;
            objDict["productName"] = productName;
            objDict["quantity"] = quantity;

            return objDict;
        }

        class func convertDataCartArrayToProductsDictionary(arrayDataCart : [DataCart]) -> Dictionary<String,AnyObject> {
            var arrayDataCartDictionaries : Array<Dictionary<String,AnyObject>> = Array()
            for objDataCart in arrayDataCart {
                arrayDataCartDictionaries.append(objDataCart.dictionary);
            }
            return ["products" : arrayDataCartDictionaries];
        }

        }

使用方法如下。

let arrayDataCarts : [DataCart] = //value of your array
//pass it to class function and you will get your result

let productsDict = let productsDict = DataCart.convertDataCartArrayToProductsDictionary(arrayDataCarts);

答案 1 :(得分:2)

    var allDictionaries : [[String : AnyObject]]//array of all dictionaries.

    func convertArrayToDictionaries([DataCart]) {
       for data in DataCart {
          let  dictionary = [
             "icon" : data.icon,
             "cartId" : data.cartId,
             "price" : data.price,
             "productName" : data.productName,
             "quantity" : data.quantity
          ]
          allDictionaries.append(dictionary)
       }
 }