Home > Net >  in dictionary (get need motherland student key valueType) error of expression is ambiguous without m
in dictionary (get need motherland student key valueType) error of expression is ambiguous without m

Time:09-09

var school : [[String:Any]] =               [[[["school1" : "devashya",
                                             "address" : "new_nikol",
                                             "student" : [[
                                             "name"   : "chirag",
                                             "roll_number" : "10"],
                                            ["name"    : "hiren",
                                             "roll_number" : "20"],
                                            ["name"     : "roshan",
                                             "roll_number"  : "30"]]]],
                                             [["school2" : "sankalpschool",
                                             "address"  : "nikol",
                                             "student" : [[
                                             "name"     : "keyur",
                                            "roll-number" : "40"],
                                            ["name"     : "mukesh",
                                            "roll-number" : "50"],
                                            ["name"     : "ravi",
                                            "roll-number" : "60"]]]],
                                            [["school3" : "motherland",
                                            "address"  : "naroda",
                                            "student"  : [[
                                            "name"     : "veer",
                                            "roll-number" : "70"],
                                            ["name"     : "nirav",
                                            "roll-number" : "80"],
                                            ["name"     : "rizvan",
                                            "roll-number" : "90"]]]],
                                            [["school4" : "vip",
                                            "address"  : "newnikol",
                                            "student" : [[
                                             "name"     : "vikky",
                                             "roll-number" : "100"],
                                             ["name"     : "alpesh",
                                             "roll-number" : "110"],
                                             ["name"     : "raavi",
                                             "roll-number"  : "120"]]]],
                                             [["school5" : "chanakya",
                                             "address" : "sardarchowk",
                                             "student" : [[
                                             "name"    : "mayur",
                                             "roll-number" : "130"],
                                             ["name"    : "vrinkesh",
                                             "roll-number" : "140"],
                                             ["name"    : "annav",
                                              "roll-number" : "150"]]]]]]


var classroom = "motherland"
for hostel in school
    {
    
    if let name = hostel["student"] as? [[String: Any]]
    {
        for Arr in name
        {

            print(Arr)
        }
       
    }
    
}

CodePudding user response:

There are lot of issue with your school variable.

Replace school with the following and try your code.

var school : [[String:Any]] = [
                                ["school1" : "devashya", "address" : "new_nikol", "student" :
                                    [[ "name" : "chirag", "roll_number" : "10"],
                                     ["name" : "hiren", "roll_number" : "20"],
                                     ["name" : "roshan", "roll_number" : "30"]]],
                                ["school2" : "sankalpschool", "address" : "nikol", "student" :
                                    [[ "name" : "keyur", "roll-number" : "40"],
                                     ["name" : "mukesh", "roll-number" : "50"],
                                     ["name" : "ravi", "roll-number" : "60"]]],
                                ["school3" : "motherland", "address" : "naroda", "student" :
                                    [[ "name" : "veer", "roll-number" : "70"],
                                     ["name" : "nirav", "roll-number" : "80"],
                                     ["name" : "rizvan", "roll-number" : "90"]]],
                                ["school4" : "vip", "address" : "newnikol", "student" :
                                    [[ "name" : "vikky", "roll-number" : "100"],
                                     ["name" : "alpesh", "roll-number" : "110"],
                                     ["name" : "raavi", "roll-number" : "120"]]],
                                ["school5" : "chanakya", "address" : "sardarchowk", "student" :
                                    [[ "name" : "mayur", "roll-number" : "130"],
                                     ["name" : "vrinkesh", "roll-number" : "140"],
                                     ["name" : "annav", "roll-number" : "150"]]]
                            ]

let names = school.filter{$0["school"] as! String == "motherland"}.first?.filter{$0.key == "student"}.values
  • Related