将值对附加到字典键

时间:2015-03-10 14:20:55

标签: ios xcode swift

我正在阅读包含许多部分的文本文件。我会有一本字典:

stringArrayCompleteData['Objekt']['Mobile']    
stringArrayCompleteData['Objekt']['address']
stringArrayCompleteData['Header']['devicenumber']
stringArrayCompleteData['Header']['departmentemailaddresse']
and much more

但是在我的loob中,总是不会附加新值,值会被覆盖。然后我只有最后一个值。

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

        for singleRow in stringArray
        {
            if(singleRow != "")
            {
                switch singleRow {
                    case "#Header":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Objekt":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Baustelle":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Auftraggeber":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Architekt":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Vermittler":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Regulierer":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Versicherung":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Kontaktstellen":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    case "#Dateien":
                        arrIndexSection = singleRow.stringByReplacingOccurrencesOfString("#", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                    default:
                        //Here the multiple array would be filled
                        var arrSingleRow = singleRow.componentsSeparatedByString(";")

                        if( arrSingleRow.count > 0  )
                        {
                            if( arrIndexSection == "Kontaktstellen" )
                            {
                                //TODO: Kontaktstellen einlesen

                                //#Kontaktstellen
                                //Baustelle;0;348873;;;;0
                                //Baustelle;0;381263;;Albrecht;0815;0
                                //Regulierer/SV;0;171979;Josef;Eder;08546/911055;0
                                println( "Kontaktstellendaten" )
                                println( singleRow )
                            }
                            else if( arrIndexSection == "Dateien" )
                            {
                                //TODO: Dateien einlesen

                                //#Dateien
                                //11022015090007_BEmail_INNNUE_21102014141534.pdf; 99; Email an eva.nuhn@mbs-service.de

                                println( "Dateiendaten" )
                                println( singleRow )
                            }
                            else
                            {
                                stringArrayCompleteData[arrIndexSection] = [arrSingleRow[0]: arrSingleRow[1]]

//这里我尝试更新值并附加值
//stringArrayCompleteData[arrIndexSection]?.updateValue(arrSingleRow [1],forKey:arrSingleRow [0])                                     //错误:无法改变常量字典

                            }
                        }
                }
            }
        }
        println( stringArrayCompleteData )
        for key in stringArrayCompleteData.keys {
            println("Key: \(key)")
        }

以下是println()

的结果
  

[Auftraggeber:[Telefon2:],Regulierer:[Telefon2:],Baustelle:[Telefon2:],Versicherung:[Telefon2:01805],标题:[iPadNr:3E24-45B8-A2AC-7E3501BED78A],Objekt:[ WeitererHinweis:测试]]

     

Key:Auftraggeber

     

Key:Regulierer

     

Key:Baustelle

     

Key:Versicherung

     

键:标题

     

Key:Objekt

1 个答案:

答案 0 :(得分:0)

我无法完全理解你在做什么但是...为什么你要使用一个字典,其中的值是另一个字典?相反,使用数组字典会更好吗?

这样的东西
 Dictionary<String, Array<Any>>()

或更好

   [String:[Any]]()

通过这种方式,词典的关键是&#34;部分名称&#34;并且Array的值(您可以使用.append方法添加)只是值。

希望这有帮助,如果没有尝试澄清您对代码的期望输入和输出,我知道您不是母语人士,但很难理解您真正需要的内容。

再见! d。