我将所选数据从列表传递到具有相应内容的模型类。我有itemName和itemPrice以及itemDescription,我将其传递给类中的模型变量并访问另一个类中的模型变量。现在我想从该模型中获取值并将其作为参数传递给服务。我在我的参数中有一些键,我想传递这些值,值是动态的。这是我的模型类,
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() {}
}
这就是我在模型中传递值的方式,
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)
这是我的服务中的参数格式,
"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!
]
],
我想从我的模型类中传递参数中的所有内容,并且值应该在参数中找到它们各自的键。我很困惑,我怎么能在各自的键中传递它,当我通过这里时index 0它只向我的参数中的子项传递0个索引值。我如何通过我的模型类中的所有东西?如果我的模型类中有两个项目,当我将数据传递给它时,它应该在我的参数中生成两个子项.Chid是一个参数中的数组。如何做到这一点?
答案 0 :(得分:0)
你可以试试这个:
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)