{"id":2883,"date":"2014-03-09T05:06:54","date_gmt":"2014-03-09T05:06:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/09\/creating-nsarray-exc_bad_access-collection-of-common-programming-errors\/"},"modified":"2014-03-09T05:06:54","modified_gmt":"2014-03-09T05:06:54","slug":"creating-nsarray-exc_bad_access-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/09\/creating-nsarray-exc_bad_access-collection-of-common-programming-errors\/","title":{"rendered":"Creating NSArray &#8211; EXC_BAD_ACCESS?-Collection of common programming errors"},"content":{"rendered":"<p>When I try to create the <code>NSArray purchasedTimeArray<\/code>, I get a crash <code>EXC_BAD_ACCESS<\/code><\/p>\n<pre><code>NSString *blank = @\"\";\nNSArray *purchasedTimeArray = [[NSArray alloc] initWithObjects:\n                                   timeRemainingTitle, blank, @\"imagename.png\", blank, description, 4, nil];\n<\/code><\/pre>\n<p>And yes, <code>timeRemainingTitle<\/code> and <code>description<\/code> still exist in memory. They are both NSStrings.<\/p>\n<ol>\n<li>\n<p>Use the new Objective-C syntax:<\/p>\n<pre><code>NSArray *purchasedTimeArray = @[ timeRemainingTitle, blank, @\"imagename.png\", blank, description, @4 ];\n<\/code><\/pre>\n<p>Two benefits:<\/p>\n<ol>\n<li>Much easier to write.<\/li>\n<li>No need for <code>nil<\/code> terminator and if one of the other values are <code>nil<\/code> in value, all the objects still end up in the array.<\/li>\n<\/ol>\n<p>As noted in the comments, the <code>@4<\/code> syntax translates to <code>[NSNumber numberWithInt:4]<\/code>.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>When I try to create the NSArray purchasedTimeArray, I get a crash EXC_BAD_ACCESS NSString *blank = @&#8221;&#8221;; NSArray *purchasedTimeArray = [[NSArray alloc] initWithObjects: timeRemainingTitle, blank, @&#8221;imagename.png&#8221;, blank, description, 4, nil]; And yes, timeRemainingTitle and description still exist in memory. They are both NSStrings. Use the new Objective-C syntax: NSArray *purchasedTimeArray = @[ timeRemainingTitle, blank, @&#8221;imagename.png&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2883","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2883"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2883\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}