ios - 在适当的索引处更新字典中的值

标签 ios swift nsdictionary

我有一个字典,它有一个键 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/

相关文章:

iphone - 尝试使用 AdMob 运行应用时出现奇怪的错误

ios - 使用 libxlsxwriter 在 iOS 中导出到 Excel 失败

javascript - 延迟加载图像在 iPhone 浏览器中不起作用

ios - 遍历两个自定义数组并在变量相等时设置值 Swift

ios - 使用 UISearchController 通过 API 进行搜索?

ios - 如何将分段控件添加到导航栏? (iOS)

ios - iOS,如何在数组中的dic中循环dic以替换空字符串的空值

ios - 如何将 UIImage 转换为 base64 和 POST 到 URL

cocoa - Mac OS X 10.7.3 添加 -[NSDictionary stringForKey :] method?

json - 使用 Decodable 解析嵌套的 JSON