Christian Niles

  1. NSIndexSet block enumeration gotcha

    If you use NSIndexSet’s block enumerators in your iOS or Mac app code, you should beware of a bug I’ve encountered when using [NSIndexSet enumerateIndexesInRange:options:block:].

    In short: it will barf and throw an exception if you provide it a range of {0, 0}. It will not barf on {1, 0}, or other ranges, which makes it insidiously easy to let this bug affect your own code.

    I’ve submitted a radar ticket, but in the meantime if you use these methods you’ll need to add a check for this case, like so:

    NSIndexSet * indexSet = [NSIndexSet indexSet];
    NSRange range = NSMakeRange(0, 0);
    if (range.length > 0)
    {
      [indexSet enumerateIndexesInRange:range
                                options:0
                             usingBlock:^(NSUInteger idx, BOOL *stop) {
                               // carry on...
                             }];
    }
    
    • text
    • Permalink
    • 1 year ago
    • Share
    • iOS
    • mac
    • cocoa
    • foundation

About Christian Niles

I'm currently working on Pivotal Tracker for iOS, which Pivotal Labs acquired from me in June 2011. I've previously led product and engineering efforts at Get Satisfaction, LinkedIn Events, and Outside.in.

About Christian Niles

I'm currently working on Pivotal Tracker for iOS, which Pivotal Labs acquired from me in June 2011. I've previously led product and engineering efforts at Get Satisfaction, LinkedIn Events, and Outside.in.

Latest From Twitter

Latest From Instagram

    'Focus (1.9)' by mikedidthis