无法将类型'__NSArrayI'(0x10df73c08)的值强制转换为'NSDictionary'(0x10df74108) [英] Could not cast value of type '__NSArrayI' (0x10df73c08) to 'NSDictionary' (0x10df74108)

查看:144
本文介绍了无法将类型'__NSArrayI'(0x10df73c08)的值强制转换为'NSDictionary'(0x10df74108)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    let url = "http://xyz/index_main.php?c=webservices&a=get&e=007"

    Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
            //to get status code
            if let status = response.response?.statusCode {
                switch(status){
                case 201:
                    print("example success")
                default:
                    print("error with response status: \(status)")
                }
            }
            //to get JSON return value
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                print(JSON)
            }

结果:

    {
    "create_by" = 007;
    "create_date" = "2016/06/20";
    "due_date" = "2016/06/22";
    "estimate_time" = "";
    fixer = 007;
    priority = High;
    "project_code" = testing;
    status = "Re-Open";
    "task_id" = 228;
    "task_title" = test;
    tester = 007;
},
    {
    "create_by" = 006;
    "create_date" = "2016/06/23";
    "due_date" = "2016/06/24";
    "estimate_time" = "";
    fixer = 007;
    priority = Critical;
    "project_code" = "tanteida.c";
    status = "In Progress";
    "task_id" = 234;
    "task_title" = testing;
    tester = 006;
}

我想转换为NSDictionary并获得类似task_id(array)的其他数组,但是出现此错误:

I want to convert to NSDictionary and get different Array like task_id(array) but I get this error:

无法将类型'__NSArrayI'的值强制转换为NSDictionary"

"Could not cast value of type '__NSArrayI' to NSDictionary"

请帮助我

提前谢谢

推荐答案

您的响应是DictionaryArray而不是Dictionary,因此,如果要使用所有字典,都可以使用循环访问它.

Your response is of Array of Dictionary not Dictionary, so if you want all the dictionary you can access it using loop.

if let array = response.result.value as? [[String: Any]] {
    //If you want array of task id you can try like
    let taskArray = array.flatMap { $0["task_id"] as? String }
    print(taskArray)
}

这篇关于无法将类型'__NSArrayI'(0x10df73c08)的值强制转换为'NSDictionary'(0x10df74108)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆