更新字典中的值
Updating value in a dictionary
我有一个 [String:Any]()
类型的字典,并且正在向它添加一个值,就像这样...
myDictionary["qty"] = "1"
现在 qty
是我可以从选择器视图更改的数量。因此,如果我 select 来自 pickerview 的值为 3,更新的数量现在将是 3 而不是 1。我正在努力实现这样...
myDictionary.updateValue(cell.qtyPickerField.text!, forKey: "qty")
arrayOfDictionary.append(myDictionary)
但是在这里,因为我正在使用追加,所以数组中又添加了一个字典,其数量值为 3。但是我想要实现的是用该值更新我已经拥有的字典3 个而不是 1 个,而不是像我现在所做的那样再添加一本字典。
但是我无法弄清楚如何实现...
编辑。选择器视图didSelectRow中的代码如下...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView is MyPickerView {
if let cell = (pickerView as! MyPickerView).cell {
cell.qtyPickerField.text = noOfItems[row] // for displaying of nos picker view
myDictionary.updateValue(cell.qtyPickerField.text!, forKey: "qty")
arrayOfDictionary.append(self.appDelegate.myDictionary)
let data = try! JSONSerialization.data(withJSONObject: arrayOfDictionary, options: .prettyPrinted)
print(data)
self.appDelegate.jsonValue = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
print(self.appDelegate.jsonValue)
}
}
}
}
首先从arrayOfDictionary
获取字典:
let index : Int = // index for myDictionary
myDictionary = arrayOfDictionary[index]
然后
myDictionary["qty"] = cell.qtyPickerField.text!
arrayOfDictionary[index] = myDictionary
swift中的Array/Dictionary是值类型,你要的是act dictionary作为引用类型。在您的情况下使用 NSMutableDictionary
例如
let dict = NSMutableDictionary()
var array = [NSMutableDictionary]()
dict["qty"] = "1"
array.append(dict)
print("Array has:\(array)")
print("------ Now we'll change the data withoout appending -----")
dict["qty"] = "3"
print("Array has:\(array)")
控制台日志:
Array has:[{
qty = 1;
}]
------ Now we'll change the data withoout appending -----
Array has:[{
qty = 3;
}]
我有一个 [String:Any]()
类型的字典,并且正在向它添加一个值,就像这样...
myDictionary["qty"] = "1"
现在 qty
是我可以从选择器视图更改的数量。因此,如果我 select 来自 pickerview 的值为 3,更新的数量现在将是 3 而不是 1。我正在努力实现这样...
myDictionary.updateValue(cell.qtyPickerField.text!, forKey: "qty")
arrayOfDictionary.append(myDictionary)
但是在这里,因为我正在使用追加,所以数组中又添加了一个字典,其数量值为 3。但是我想要实现的是用该值更新我已经拥有的字典3 个而不是 1 个,而不是像我现在所做的那样再添加一本字典。
但是我无法弄清楚如何实现...
编辑。选择器视图didSelectRow中的代码如下...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView is MyPickerView {
if let cell = (pickerView as! MyPickerView).cell {
cell.qtyPickerField.text = noOfItems[row] // for displaying of nos picker view
myDictionary.updateValue(cell.qtyPickerField.text!, forKey: "qty")
arrayOfDictionary.append(self.appDelegate.myDictionary)
let data = try! JSONSerialization.data(withJSONObject: arrayOfDictionary, options: .prettyPrinted)
print(data)
self.appDelegate.jsonValue = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
print(self.appDelegate.jsonValue)
}
}
}
}
首先从arrayOfDictionary
获取字典:
let index : Int = // index for myDictionary
myDictionary = arrayOfDictionary[index]
然后
myDictionary["qty"] = cell.qtyPickerField.text!
arrayOfDictionary[index] = myDictionary
swift中的Array/Dictionary是值类型,你要的是act dictionary作为引用类型。在您的情况下使用 NSMutableDictionary
例如
let dict = NSMutableDictionary()
var array = [NSMutableDictionary]()
dict["qty"] = "1"
array.append(dict)
print("Array has:\(array)")
print("------ Now we'll change the data withoout appending -----")
dict["qty"] = "3"
print("Array has:\(array)")
控制台日志:
Array has:[{
qty = 1;
}]
------ Now we'll change the data withoout appending -----
Array has:[{
qty = 3;
}]