ios - 向字典数组添加数据

标签 ios xcode swift

目的是为文本文件的全部内容创建一个多维数组,将导入到 CoreData 中。

文本文件有节,节下是具有表属性和值的行。在开关的默认下,是一个简短的示例。

我收到以下错误:

Could not find member 'subscript'

var stringArray = fullImportContent!.componentsSeparatedByString("\n")
var stringArrayCompleteData = Dictionary<String, Dictionary<String, Any>>()
var arrIndexSection : String

for singleRow in stringArray
{
    if(singleRow != "")
    {
        switch singleRow {
                    case "#Header":
                        arrIndexSection = singleRow
                    case "#Objekt":
                        arrIndexSection = singleRow
                    case "#Baustelle":
                        arrIndexSection = singleRow
                    case "#Auftraggeber":
                        arrIndexSection = singleRow
                    case "#Architekt":
                        arrIndexSection = singleRow
                    case "#Vermittler":
                        arrIndexSection = singleRow
                    case "#Kontaktstellen":
                        arrIndexSection = singleRow
                    case "#Dateien":
                        arrIndexSection = singleRow
                    default:
                        //Here the multiple array would be filled
                        var arrSingleRow = singleRow.componentsSeparatedByString(";")
                        /*
                        Example how the Context could be in the textfile
                        #Objekt
                        Objektnr; 1000000;
                        */

                        stringArrayCompleteData += [arrIndexSection][arrSingleRow[0]][arrSingleRow[1]]
                        println(singleRow)
        }
    }
}

如何将“默认”中的行添加到 CompleteData 数组?

最佳答案

stringArrayCompleteData定义为 Dictionary<String, Dictionary<String, Any>> ,你需要像这样添加一个键值对:

stringArrayCompleteData[arrIndexSection] = [arrSingleRow[0]: arrSingleRow[1]]

关于ios - 向字典数组添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958229/

相关文章:

ios - 无法从核心数据加载 SwiftUI 选择器选择并显示为选择

ios - 当我尝试使用 userDefaults 保存到 ui 颜色时,Xcode 给我错误 : Fatal error: Unexpectedly found nil while unwrapping an Optional value

ios - SwiftUI:不推荐使用 NavigationDestinationLink

ios - 将框阴影放在不显示的 UITableView 单元格上 [Swift 3]

ios - 在 Firebase 身份验证中使用 Google 登录后如何顺利地执行 segue 到 View Controller

ios - 替换代码中的 IBOutlet View

ios - 标签栏在 segue 上消失

ios - 在 Objective-C 的 UIImage 的透明区域上屏蔽背景颜色

ios - 如何测试 ios11 提升的应用内购买

ios - 委托(delegate)如何在 objective-c 中工作?