如何在Swift 4中基于字典的键名添加数组

时间:2018-06-13 10:45:08

标签: ios swift

我正在尝试在我的词典中添加数组。

enter image description here

如何在 Details1 ProductDetails 中添加 Details2 所以我可以在TableView上显示每行包含集合视图。

(((((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)

这是正确的解决方案,或者我可以使用任何其他方式

2 个答案:

答案 0 :(得分:1)

您可以获取详细信息的每个元素1,如

var new = details1[0]

现在添加

new["productDetails"] = details2

并将新数据添加到主数组中,如

details[0] = new

答案 1 :(得分:0)

使用Example

将JSON转换为模型类或结构的简便方法

然后你可以这样做

{{1}}