我正在尝试在我的词典中添加数组。
如何在 Details1 的 ProductDetails 中添加 Details2 这样我就可以在 TableView 上显示每一行都包含 Collection View 。
(((((self.completeDetailsArray[index] as! NSDictionary).mutableCopy()) as! NSMutableDictionary).value(forKey: "productDetails") as! NSArray).mutableCopy() as! NSMutableArray).addObjects( from: productArray )
我试过这个,但不起作用
let productArray : NSArray = responseResult.value(forKey: "details2") as! NSArray
let oldProductDict : NSMutableDictionary = (self.completeDetailsArray[index] as! NSDictionary).mutableCopy() as! NSMutableDictionary
let oldProductArray : NSArray = oldProductDict.value(forKey: "productDetails") as! NSArray
let completeProductArray = oldProductArray.addingObjects(from: productArray as! [Any])
oldProductDict.setValue(completeProductArray, forKey: "productDetails")
self.completeDetailsArray.replaceObject(at: index, with: oldProductDict)
let oldProductDict2 : NSDictionary = self.completeDetailsArray[index] as! NSDictionary
print("oldProductDict2",oldProductDict2)
这是有效的,这是正确的解决方案还是我可以使用任何其他方式
最佳答案
你可以像这样获取details1的每个元素
var new = details1[0]
现在添加
new["productDetails"] = details2
并将新的添加到主数组中
details[0] = new
关于ios - 如何在 Swift 4 中基于字典的键名添加数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835237/