Ios 当NSDictionary[]抛出EXC_BAD_指令运行时错误时,JSON的数据转换结果
在作为NSDictionary解析后,我在Swift代码中收到了以下JSON。在我的函数中,我试图将“results”块中的JSON对象提取为NSDIctionary[],但这会引发运行时错误。我不明白为什么,因为就在几天前,它还在工作Ios 当NSDictionary[]抛出EXC_BAD_指令运行时错误时,JSON的数据转换结果,ios,json,swift,Ios,Json,Swift,在作为NSDictionary解析后,我在Swift代码中收到了以下JSON。在我的函数中,我试图将“results”块中的JSON对象提取为NSDIctionary[],但这会引发运行时错误。我不明白为什么,因为就在几天前,它还在工作 { "results": [ { "id": "3", "name": "The National", "slug": "thenational", "facebook_url": "htt
{
"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 lib解析后接收到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[]
更新:
我打印了results对象的类,因为它返回为uu NSCFDictionary
这里有一个关于这一点的讨论,说要像NSDictionary一样使用它,但在我的例子中它不起作用。结果的值在JSON中不是字典,而是数组。你应该用这样的东西得到它
let allResults: NSArray = results["results"] as NSArray
请提供一段代码,说明如何使用swiftTry在转换之前打印出其类来解析此json。尝试在转换之前打印'jsonResult',它是否有'results'键,这意味着它是一个字典,而不是一个字典数组。查找“转换”,这不是您要做的。您正在执行从JSON字节到NSDictionary-r
NSArray
的数据转换。我已尝试将其转换为NSArray,但这也会引发运行时错误。什么?它不是一个数组数组,是吗?如果有的话,它应该是一个单一的NSArray
,而不是它们的字典@srinivas尝试不使用[]
@ujell嘿,谢谢!真棒,摆脱了运行时错误。非常感谢。