Dharini Dharini - 9 days ago 5
iOS Question

save response in nsmutabledictionary ? swift 3.0

My code for getting response in Alamofire 4.0

Alamofire.request(webpath).responseJSON
{
response in

//here I want to store response in nsmutabledictionary
}


And within that response I have Data object , which contains data like below

{"Success":"True","Data":[{"Id":"4","Name":"xyz"}]


I want to save that Data object in
NSMutableArray
for displaying in
UITableview

Answer

You can get result Dictionary from Alamofire response like this.

Alamofire.request(webpath).responseJSON
{
    response in

    switch(response.result) {
    case .success(_):
        if let dic = response.result.value as? [String: Any], 
           let array = dic["Data"] as? [[String: Any]] {
            print(array)
        }
    case .failure(_):
        print(response.result.error)
    }
}

Note: Use Swift native Dictionary and Array instead of NSDictionary and NSArray.