繁体   English   中英

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

[英]How to Add Array In Dictionary Based Key Name In Swift 4

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

在此处输入图片说明

如何添加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)

这正在工作是正确的解决方案,还是我可以使用其他任何方式

您可以获取details1的每个元素,例如

var new = details1[0]

现在添加

new["productDetails"] = details2

并添加新到主数组像

details[0] = new

使用JSONExport将JSON转换为模型类或结构的简单方法

那么你可以像这样

let details2:[ProductDetails] = [ProductDetails]()
details1.productDetails = details2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM