{"id":172,"date":"2013-04-20T12:31:16","date_gmt":"2013-04-20T12:31:16","guid":{"rendered":"http:\/\/ootips.org\/yonat\/?p=172"},"modified":"2016-04-17T07:14:02","modified_gmt":"2016-04-17T07:14:02","slug":"workaround-for-bug-in-nsindexset-shiftindexesstartingatindex","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/workaround-for-bug-in-nsindexset-shiftindexesstartingatindex\/","title":{"rendered":"Workaround for bug in [NSMutableIndexSet shiftIndexesStartingAtIndex:by:]"},"content":{"rendered":"<h3>The Bug<\/h3>\n<p>Shifting an <tt>NSMutableIndexSet<\/tt> by a negative number will drop an index in some cases.<\/p>\n<p>Example Code:<\/p>\n<pre class=\"lang:objc decode:true \">\r\nNSMutableIndexSet *set = [NSMutableIndexSet indexSetWithIndex:0];\r\n[set addIndex:2];\r\n[set shiftIndexesStartingAtIndex:1 by:-1];\r\nNSLog(@\"%@\", set);\r\n<\/pre>\n<p>The set should contain 0-1 but instead contains only 1.<\/p>\n<h3>The Reason<\/h3>\n<p><tt>NSIndexSet<\/tt> is a series of <tt>NSRange<\/tt>-s. If the shift method removes empty space between ranges, than they should become a single unified range. For example, if a set contains the range 1-2 and the range 5-6, and we do<\/p>\n<pre class=\"lang:objc decode:true \">\r\n[set shiftIndexesStartingAtIndex:3 by:-2];\r\n<\/pre>\n<p>then we should get a set with a single range 1-4.<\/p>\n<p>However, the implementation of <tt>shiftIndexesStartingAtIndex:by:<\/tt> fails to unify ranges, and also assumes that separate ranges have at least one empty space between them. And so we get a set containing the ranges 1-1 and 3-4.<\/p>\n<h3>The Workaround<\/h3>\n<p>Luckily, the methods <tt>addIndex:<\/tt> and <tt>addIndexesInRange:<\/tt> do correctly unify ranges. And so the workaround is to first call one of these methods, and only then shift:<\/p>\n<pre class=\"lang:objc decode:true \">\r\n[set addIndexesInRange:NSMakeRange(3, 2)];\r\n[set shiftIndexesStartingAtIndex:3 by:-2];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Bug Shifting an NSMutableIndexSet by a negative number will drop an index in some cases. Example Code: NSMutableIndexSet *set = [NSMutableIndexSet indexSetWithIndex:0]; [set addIndex:2]; [set shiftIndexesStartingAtIndex:1 by:-1]; NSLog(@&#8221;%@&#8221;, set); The set should contain 0-1 but instead contains only 1. &hellip; <a href=\"https:\/\/ootips.org\/yonat\/workaround-for-bug-in-nsindexset-shiftindexesstartingatindex\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[13,14,16],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-objective-c","tag-sample-code","tag-workaround"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-2M","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":12,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/172\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}