{"id":1900,"date":"2014-03-25T17:53:08","date_gmt":"2014-03-26T00:53:08","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1900"},"modified":"2014-10-02T13:07:53","modified_gmt":"2014-10-02T20:07:53","slug":"removing-items-from-nsmutablearrays","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1900","title":{"rendered":"Removing items from NSMutableArray&#8217;s"},"content":{"rendered":"<p>I have an NSMutableArray in Objective-C that I want to remove items from so that it has just the number of items that will be displayed in a grid on the screen. The number of items varies, but on one screen it is 9. The array has more items, but I don\u2019t necessarily know how many. [Remember that array items are numbered with an index starting at 0. So the last index number of my array of 9 items is 8&#8242;]<\/p>\n<p>This is the original code that I tried.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSUInteger itemsOnScreen = 9;\nfor (NSUInteger i = itemsOnScreen; i &lt; gridItems.count; i++) {\n&nbsp;&nbsp;&nbsp;&nbsp;[gridItems removeObjectAtIndex:i];\n}\n<\/code><\/pre><\/p>\n<p>This code doesn\u2019t delete all of the items so I decided to start at the end and delete items until I there were only 9 left. This is the code that I ended up using.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSUInteger itemsOnScreen = 9;\n\nfor (NSUInteger i = gridItems.count - 1; i &gt; itemsOnScreen - 1; i--) {\n&nbsp;&nbsp;&nbsp;&nbsp;[gridItems removeObjectAtIndex:i];\n}\n<\/code><\/pre><\/p>\n<p>It\u2019s kind of ugly and after working with it it occurred to me why the original code didn\u2019t work. As I started deleting items, the number of items, and hence the index of the last item changed. So it worked fine for the first few, but as I neared the end of the loop, there were no items with an index that matched i. If I just delete the first item after the 9 that I want, then the loop works fine.<br \/>\n<pre><code class=\"\u2019smaller\u2019 preserve-code-formatting\">\nNSUInteger itemsOnScreen = 9;\nfor (NSUInteger i = 0; i &lt; gridItems.count; i++) {\n&nbsp;&nbsp;&nbsp;&nbsp;[gridItems removeObjectAtIndex:itemsOnScreen];\n}\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have an NSMutableArray in Objective-C that I want to remove items from so that it has just the number of items that will be displayed in a grid on the screen. The number of items varies, but on one screen it is 9. The array has more items, but I don\u2019t necessarily know how &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1900\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Removing items from NSMutableArray&#8217;s<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1900","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1900"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1900\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}