简体   繁体   English

从Swift中的模型类中提取值

[英]Extract values from an model class in Swift

i'm passing my selected data from a list to a model class with there respective things. 我将选择的数据从列表传递到具有相应内容的模型类。 i have itemName and itemPrice and itemDescription which i pass to my model variables in a class and access that in another class. 我有itemName和itemPrice以及itemDescription,它们传递给我的模型变量在一个类中并在另一个类中访问它。 Now i want to get values out of that model and pass it to service as a parameter. 现在,我想从该模型中获取值,并将其作为参数传递给服务。 i have some keys in my parameter in which i want to pass these values, the values are dynamic. 我在参数中有一些要传递这些值的键,这些值是动态的。 This is my model class, 这是我的模特班

class Item : NSObject {
var totalPrice : String?
var numberOfItems : Int = 1
var itemPrice : String?
var itemName : String?
var itemID : String?
var itemId : String?
var  itemDdescription : String?
}

class ItemDataSource : NSObject {
var items = [Item]()
static let sharedInstance = ItemDataSource()
private override init() {}
}

This is how i'm passing values in my model, 这就是我在模型中传递值的方式,

 let item = Item()
    item.totalPrice = String(result)
    item.itemPrice = String(result)
    item.itemDdescription = itemDescription
    item.itemName = itemName
    item.itemID = itemID
    item.itemId = itemId
    ItemDataSource.sharedInstance.items.append(item)

this is my parameter format that are going in my service , 这是我在服务中使用的参数格式,

 "cart": [
            [
                "childs": [
                    [
                        "addon_cat_id": "0",
                        "id": ItemDataSource.sharedInstance.items[0].itemId!,
                        "name": ItemDataSource.sharedInstance.items[0].itemName!,
                        "next_move_id": "",
                        "price": ItemDataSource.sharedInstance.items[0].itemPrice!,
                        "sort_order": "",
                        "type": "string"
                    ]
                ],
                "name": ItemDataSource.sharedInstance.items[0].itemName!,
                "price": ItemDataSource.sharedInstance.items[0].itemPrice!,
                "productid": ItemDataSource.sharedInstance.items[0].itemId!,
                "qty": 2,
                "description": ItemDataSource.sharedInstance.items[0].itemDdescription!
            ]
        ],

I want to pass all that things in parameter from my model class,and values should go to their respective keys in parameters.I'm confused that how can i pass that in their respective keys and when i pass here with index 0 it passes only 0 index values to the childs in my parameter. 我想从我的模型类中传递参数中的所有内容,而值应该传递给它们在参数中的相应键。我很困惑如何在其各自的键中传递值,而当我在此处通过索引0传递时,它只能传递0我的参数中的孩子的索引值。 How can i pass my whole things that are in my model class?. 我如何传递我的模型课中的全部内容? if there are two items in my model class that it should make two childs in my parameter when i pass my data to it.Chid is an array that is going in parameter. 如果我的模型类中有两个项目,当我将数据传递给它时,它应该在我的参数中产生两个孩子.Chid是一个传入参数的数组。 How this can be done? 如何做到这一点?

You can try this: 您可以尝试以下方法:

class Item : NSObject {
    var totalPrice : String?
    var numberOfItems : Int = 1
    var itemPrice : String?
    var itemName : String?
    var itemID : String?
    var  itemDdescription : String?
}

class ItemDataSource : NSObject {
    var items = [Item]()
    static let sharedInstance = ItemDataSource()
    private override init() {}
}
var result = 20
var itemDescription = "itemDescription"
var itemName = "itemName"
var itemID = "1"

let item = Item()
item.totalPrice = String(result)
item.itemPrice = String(result)
item.itemDdescription = itemDescription
item.itemName = itemName
item.itemID = itemID
ItemDataSource.sharedInstance.items.append(item)

let noOFItems = ItemDataSource.sharedInstance.items.count

var dict = [Dictionary<String, Any>]()


for i in 0 ..< noOFItems {


    var child: [String: Any] = ["addon_cat_id": "0",
                                "id": ItemDataSource.sharedInstance.items[i].itemID!,
                                "name": ItemDataSource.sharedInstance.items[i].itemName!,
                                "next_move_id": "",
                                "price":ItemDataSource.sharedInstance.items[i].itemPrice!,
                                "sort_order": "",
                                "type": "string"]

    var childs:[String: Any] = ["childs": child, "name": ItemDataSource.sharedInstance.items[i].itemName!,
                                "price": ItemDataSource.sharedInstance.items[i].itemPrice!,
                                "productid": ItemDataSource.sharedInstance.items[i].itemID!,
                                "qty": 2,
                                "description": ItemDataSource.sharedInstance.items[i].itemDdescription!]

    dict.append(childs)

}

let cart = ["cart": dict]
print(cart)

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

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