Home > Enterprise >  Execution was interrupted, reason: signal SIGABRT. 2D Array with dictionary-swift
Execution was interrupted, reason: signal SIGABRT. 2D Array with dictionary-swift

Time:05-20

var sectionArray:[[String:Any]] = [ ["sectionName":"Time Cards To Approve","sectionData":
                    [["fname":"true detective","date":"may 20"],["fname":"abbas","date":"may 10"]],"expanaded":false],
                 ["sectionName":"Message Log","sectionData":[["movie":"true detective","event":"Bring food","date":"May 19"],["movie":"false detective","event":"no shoot today","date":"may 20"]],"expanaded":false]
                 ]

I want to get the fname when I try below code it shows the error like **error: Execution was interrupted, reason: signal SIGABRT. The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. **

let ni = sectionArray[0]
let mi = ni["sectionData"] as! [String:String]
let sh = mi["fname"]

CodePudding user response:

you cannot cast ni["sectionData"] as! [String:String] because it is not an array of [String:String] and you should not use !.

Try this:

if let mi = ni["sectionData"] as? [[String:String]] {
    print("---> mi: \(mi) ")

    let sh1 = mi[0]["fname"]
    let sh2 = mi[1]["fname"]

    print("---> sh1: \(sh1) \n")
    print("---> sh2: \(sh2) \n")
}

you can of course also use a for loop, such as:

if let mi = ni["sectionData"] as? [[String:String]] {
    for i in mi.indices {
        print("---> sh: \(mi[i]["fname"]) ")
    }
}
  • Related