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. 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. 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. 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.