{"id":3969,"date":"2022-08-21T00:25:45","date_gmt":"2022-08-20T18:55:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/"},"modified":"2022-08-21T00:25:45","modified_gmt":"2022-08-20T18:55:45","slug":"solved-accessing-values-from-nsarray-duplicate","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/","title":{"rendered":"[Solved] Accessing values from NSArray [duplicate]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-32286339\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"32286339\" data-parentid=\"32285650\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>As others have said, the variable myarray contains a dictionary.<\/p>\n<p>To explain fully:<\/p>\n<p>In Objective-C, a variable that contains an object actually contains a pointer to that object. Think of it as an entry in your program&#8217;s rolodex.<\/p>\n<p>The variable has a type, &#8220;pointer to array&#8221;. It points to an object that <em>should be<\/em> an array.<\/p>\n<p>However, it is possible to lie to the compiler, and have the variable point to something that is not actually an array.<\/p>\n<h2>EDIT:<\/h2>\n<p>To continue the rolodex analogy, it&#8217;s as if you have an entry in your rolodex for a plumber, but the phone number (the pointer) is actually the phone number for a roofer. You call the number and start asking the person that answers about plumbing. The roofer doesn&#8217;t understand what you are talking about, and hangs up. (The conversation crashes.)<\/p>\n<p>The phone number is the pointer, of the wrong type. The pointer of type plumber actually points to an instance of the roofer class, so bad things happen when you start sending plumber messages to the roofer.<\/p>\n<p>Take this code for example:<\/p>\n<pre><code>- (void) typeCasting;\n{\n\n  NSDictionary* aDict = \n  {\n    @\"key1\": @\"value1\",\n    @\"key2\": @\"value2\",\n  }  \/\/ -1-\n  NSArray *anArray;\n\n  \/\/The compiler throws an error here.\n  anArray1 = aDict;  \/\/-2-\n\n  \/\/The compiler allows this due to typecasting.       \n  anArray2 = (NSArray *) aDict;   \/\/-3-\n\n  \/\/This code compiles but crashes at runtime.\n  NSString *aString = anArray2[0];  \/\/-4-\n\n  \/\/typecast back to dict\n  NSDictionary *anotherDict = (NSDictionary *) anArray2; \/\/-5-\n}\n<\/code><\/pre>\n<p>At the line marked <code>-1-<\/code> above, we create a dictionary.<\/p>\n<p>At <code>-2-<\/code> we try to assign our dictionary to a new variable of type <code>NSArray*<\/code>. The compiler complains, as it should. This is a programming error that the compiler prevents.<\/p>\n<p>At <code>-3-<\/code>, we commit the same error, but this time we add the <code>(NSDictionary*)<\/code> typecast, which tells the compiler &#8220;Trust me, I know what I&#8217;m doing.&#8221; This is setting us up for runtime crashes later.<\/p>\n<p>At <code>-4-<\/code> we try to index into <code>anArray2<\/code>, but it doesn&#8217;t really contain an array, it contains a dictionary. We crash.<\/p>\n<p>My guess is that somewhere in your code, you have a line like <code>-3-<\/code> where you cast a dictionary to an array, and set yourself up for your crash. The <strong>correct<\/strong> fix is to not do that in the first place. However, you could &#8220;band-aid&#8221; it after the fact by re-casting your <code>myarray<\/code> back to a dictionary, as shown in <code>-5-<\/code> above.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Accessing values from NSArray [duplicate] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] As others have said, the variable myarray contains a dictionary. To explain fully: In Objective-C, a variable that contains an object actually contains a pointer to that object. Think of it as an entry in your program&#8217;s rolodex. The variable has a type, &#8220;pointer to array&#8221;. It points to an object that should be &#8230; <a title=\"[Solved] Accessing values from NSArray [duplicate]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/\" aria-label=\"More on [Solved] Accessing values from NSArray [duplicate]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,471,470],"class_list":["post-3969","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-ios","tag-objective-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Accessing values from NSArray [duplicate] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Accessing values from NSArray [duplicate] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] As others have said, the variable myarray contains a dictionary. To explain fully: In Objective-C, a variable that contains an object actually contains a pointer to that object. Think of it as an entry in your program&#8217;s rolodex. The variable has a type, &#8220;pointer to array&#8221;. It points to an object that should be ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T18:55:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Accessing values from NSArray [duplicate]\",\"datePublished\":\"2022-08-20T18:55:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/\"},\"wordCount\":348,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"arrays\",\"ios\",\"objective-c\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/\",\"name\":\"[Solved] Accessing values from NSArray [duplicate] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-08-20T18:55:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-accessing-values-from-nsarray-duplicate\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Accessing values from NSArray [duplicate]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Accessing values from NSArray [duplicate] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Accessing values from NSArray [duplicate] - JassWeb","og_description":"[ad_1] As others have said, the variable myarray contains a dictionary. To explain fully: In Objective-C, a variable that contains an object actually contains a pointer to that object. Think of it as an entry in your program&#8217;s rolodex. The variable has a type, &#8220;pointer to array&#8221;. It points to an object that should be ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T18:55:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Accessing values from NSArray [duplicate]","datePublished":"2022-08-20T18:55:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/"},"wordCount":348,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","ios","objective-c"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/","url":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/","name":"[Solved] Accessing values from NSArray [duplicate] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T18:55:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-accessing-values-from-nsarray-duplicate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Accessing values from NSArray [duplicate]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3969"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3969\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}