【问题标题】:Data conversion from JSON results as NSDictionary[] throws EXC_BAD_INSTRUCTION runtime error从 JSON 结果转换为 NSDictionary[] 的数据会引发 EXC_BAD_INSTRUCTION 运行时错误
【发布时间】:2014-06-24 17:36:45
【问题描述】:

在被解析为 NSDictionary 之后,我的 Swift 代码中收到了以下 JSON。在我的函数中,我试图将“结果”块中的 JSON 对象提取为 NSDIctionary[],但这会引发运行时错误。我不明白为什么,因为这只是几天前的工作。

{
"results": [
    {
        "id": "3",
        "name": "The National",
        "slug": "thenational",
        "facebook_url": "https://www.facebook.com/thenationalofficial/",
        "twitter_url": "https://twitter.com/The_National",
        "profile_image": "http://example.staging.com/media/profile_image/thumbnail_263x263/1352756032.jpg",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "5",
        "name": "Mayer Hawthorne",
        "slug": "mayerhawthorne",
        "facebook_url": "https://www.facebook.com/MayerHawthorne",
        "twitter_url": "https://twitter.com/MayerHawthorne",
        "profile_image": "http://example.example.com/media/profile_image/thumbnail_263x263/1352755133.png",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "20",
        "name": "I Play Maracas",
        "slug": "iplaymaracas",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.wedemand.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
            "_demanded_by": null,
            "demand_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=1&access_token={}",
            "dismiss_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=0&access_token={}"
        }
    },
    {
        "id": "35",
        "name": "Black SuperHeros",
        "slug": "blacksuperheros",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.example.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "49",
        "name": "Ayman Elgadi",
        "slug": "aymanelgadi",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.example.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "8874",
        "name": "Lauri",
        "slug": "lauri",
        "facebook_url": "http://www.facebook.com/hughlaurieblues",
        "twitter_url": "http://twitter.com/hughlaurieblues",
        "profile_image": "http://staging.example.com/media/profile_image/thumbnail_263x263/lauri_profilepic.jpg",
        "_type": "artist",
        "resource_uris": {
        }
    }
]
}

我的 IOS-Swift 代码在被 AFNetworking 库解析后接收到 NSDictionary 对象,并传递给将结果数组转换为 NSDictionary[] 的函数,现在抛出运行时错误,而之前这可以工作。

(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("JSON: " + responseObject.description)

var jsonResult: NSDictionary = responseObject as NSDictionary

这个 jsonResult 被传递给下面的函数,它试图转换为 NSDictionary[]

let allResults: NSDictionary[] = results["results"] as NSDictionary[]

更新: 我打印了结果对象的类,因为它作为 __NSCFDictionary 返回。

What is an NSCFDictionary? 是关于此的讨论,并说要像使用 NSDictionary 一样使用它,但在我的情况下它不起作用。

【问题讨论】:

  • 请提供一个代码如何用swift解析这个json
  • 尝试在演员表之前打印出它的类。
  • 之前尝试打印'jsonResult',是否有'results'键
  • 这意味着它是一个字典,而不是一个字典数组。
  • 查找“铸造”,这不是你在做什么。您正在执行从 JSON 字节到 NSDictionary -r NSArray 的数据转换。

标签: ios json swift


【解决方案1】:

results 的值不是 JSON 中的字典,它是一个数组。你应该用这样的东西得到它;

let allResults: NSArray = results["results"] as NSArray

【讨论】:

  • 我尝试将其转换为 NSArray,但这也会引发运行时错误。
  • 什么?不是数组数组吧?
  • 如果有的话,那将是一个NSArray,而不是它们的字典。 @srinivas 尝试不使用 []。
  • @ujell 嘿,谢谢!真棒摆脱了运行时错误。谢谢!
猜你喜欢
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
相关资源
最近更新 更多