简体   繁体   English

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

[英]Convert array data model to dictionary (swift)

Data Model 资料模型

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

  }
}

Dictionary 字典

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 {
........
}

I've array from data model that i'd like to show it as dictionary, and i got confused to change it. 我从数据模型中排列了一个数组,希望将其显示为字典,但是我很困惑地对其进行更改。 How to convert cart's array (DataCart) to dictionary? 如何将购物车的数组(DataCart)转换为字典?

It's good practice to make conversion at class level. 在班级进行转换是个好习惯。

So in future if you have more keys, you just need to change at class level only instead of changing in all the places where you have used the same. 因此,将来如果您有更多的键,则只需要在类级别进行更改,而不必在使用相同键的所有位置进行更改。

Add one calculated property dictionayDataCart in class as below. 在类中添加一个计算的属性dictionayDataCart ,如下所示。 Also add one method " convertDataCartArrayToProductsDictionary: " which will convert your array of objects to dictionary. 还添加一种方法“ 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];
        }

        }

Use method as below. 使用方法如下。

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);
    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)
       }
 }

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

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