我有一个字典,它有一个键 product_quantity
。现在 product_quantity
的值为“1”。但后来 product_quantity
从选择器 View 中获取了不同的值,那时我想要更新字典中 product_quantity
的值。换句话说,我想更新字典中的值。但是我无法弄清楚如何实现这一目标。
这样的事情到目前为止已经完成了..这里我给出的 index
刚刚初始化为 var index = Int()
并且它的值始终为0. 所以它不起作用。
希望有人能帮忙...
self.appDelegate.myDictionary["product_quantity"] = cell.qtyPickerField.text!
self.appDelegate.arrayOfDictionary[index] = self.appDelegate.myDictionary
最佳答案
为什么不使用struct
来保存您想要的数据。
struct Product {
var quantity: Int
}
然后创建这个结构的对象。
var product = Product(quantity: 0)
然后您只需要在程序稍后的某个时刻更新数量变量即可。
product.quantity = 10
更新: 刚刚看到您的编辑。
struct Product {
var quantity: Int
}
struct MyData {
var product: Product
mutating func updateProduct(quantity: Int) {
product.quantity = quantity
}
}
var product = Product(quantity: 0)
var myData = MyData(product: product)
print(myData.product.quantity)
// call the updateProduct method to update the quantity.
myData.updateProduct(quantity: 20)
print(myData.product.quantity)
关于ios - 在适当的索引处更新字典中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47427526/