ios - Could not cast value of type '__NSDictionaryM' to 'NSArray" -
this question has answer here:
this part of code:
let quotesdata = try nsjsonserialization.jsonobjectwithdata(data!, options: .mutablecontainers) as! nsdictionary var quotedictionary: [nsdictionary]! quotedictionary = quotesdata["response"] as! [nsdictionary]
this part of json tumblr api:
{ "meta": { "status": 200, "msg": "ok" }, "response": { "blog": { "title": "a sea of quotes", "name": "aseaofquotes", "total_posts": 10089, "posts": 10089, "url": "http://www.aseaofquotes.com/", "updated": 1461556819, "description": "", "is_nsfw": false, "ask": true, "ask_page_title": "thanks in advance if leave nice message and/or correct quote sources. please check faqs before sending. not respond anonymous questions, may answer anon question in faq. :) warning: may take me while reply.", "ask_anon": true, "submission_page_title": "submit quote", "share_likes": false }, "posts": [ { "blog_name": "aseaofquotes", "id": 143359277336, "post_url": "http://www.aseaofquotes.com/post/143359277336/marlon-james-a-brief-history-of-seven-killings", "slug": "marlon-james-a-brief-history-of-seven-killings", "type": "photo", "date": "2016-04-25 04:00:18 gmt", "timestamp": 1461556818, "state": "published", "format": "html", "reblog_key": "yiiqhc46",
etc.
this error:
could not cast value of type '__nsdictionarym' (0x101925d38) 'nsarray' (0x101925900).
on line:
quotedictionary = quotesdata["response"] as! [nsdictionary]
i don't understand why. i'm pretty new json , ios "response" looks dictionary me, don't know why it's nsarray, don't don't know how fix this. appreciated.
this not duplicate because other post did not me solve problem.
the error message says
could not cast actual type nsdictionary expected type nsarray
[nsdictionary]
means array of dictionaries, value of response
dictionary, represented pair of braces.
so it's actually
let quotedictionary = quotesdata["response"] as! nsdictionary
but it's recommended use swift native collection types
let quotedictionary = quotesdata["response"] as! dictionary<string,anyobject>
Comments
Post a Comment