NerdyC

Welcome to the OCD nerd power hour.

RSS
  • Posts tagged with “cocoa.”
  • Archive
  • 1 of 1

NSIndexSet block enumeration gotcha

May 10, 2012

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...
                         }];
}

Finally! A way to test-drive my Objective-C projects!

May 16, 2010

I’ve had an on-again off-again relationship with Objective-C and Cocoa. I wrote a artsy visualization in it that ended up in some museums, a rather crappy utility to transfer email between IMAP servers, and some other stuff.

But I always became really frustrated with how hard it was to test drive any of this code. Granted, test-driving graphics and UI code can be hard, but I really love the clarity BDD process gives you when you’re hacking something together. And so I eventually lost interest.

Enter MacRuby. It’s now possible to use Ruby to test drive your Objective-C code, and I’m loving it (mostly). Seriously, go read this tutorial and try it out.

I used this method to develop an XML-Pull Parser in Objective-C called Opal — something I’ve long missed in both Objective-C and Ruby. Download it, and build the ‘Test’ target to see how the specs run. The Build window doesn’t open automatically, so you’ll need to hit Command-Shift-B to open it.

You’ll see some failures there if you haven’t installed the Bacon gem for MacRuby of course:

$ sudo macgem install bacon

I’m using the tiny Bacon library to spec my code because RSpec has trouble running under Ruby 1.9, which is the version MacRuby supports. RSpec 2.0 should fix this. You can also try out other ruby testing libraries of course, though Bacon is pretty good for what I need to do.

Caveats

While I’m really happy that I’m finally able to use a truer BDD workflow in an Objective-C environment, I’m less happy with the interfaces I have to use. The specs are run during the XCode build process, so I have to pour through Bacon’s output within a giant text window:

It’s not preventing me from getting stuff done, but I spend way more time pouring through this output than I should. There’s also no way to click through to the spec failures, nor a way to run a single spec. I suspect the only way I’d get this is if I roll my own command line interfaces, probably integrated with TextMate.

Once last caveat. While MacRuby’s been a very solid way to interface with the Objective-C runtime, it’s actually not a very solid Ruby interpreter. That makes sense — they’ve been focusing on the integration of the two languages more than shipping a Ruby interpreter. MacRuby 0.6 passes 85% of the RubySpecs, but I’d still think twice before I started a MacRuby project with lots of Ruby code. Or I’d use Cocoa or Foundation libraries where I could — like NSURLConnection over Net::HTTP for example.

About the Author

Portrait photo for NerdyC

A {food, computer, language} nerd who lived in NYC for 9 years, but is now much happier in SFO.

Stuff I Like

Following:

parislemon kkr theworldwelivein photojojo ohscience timoni superamit kthread dphiffer darrellsilver nattles jkottke americandrink annielin americanmccarver dianakimball colinmeloy abangupjob john mizginevra thoughtbot html5watch chiragdave leyink jessaclark rdeeming ericsuesz visivo netflixmoviesthatdontsuck fuckyeahamitgupta lazycrafter bikebasketpies kimjoar grubbin internetlovesamit jeanniechoe clairebrain enjoywhatyoudrink materialco youaremybestfriend venturecraftrepost philco bhaggs imnotdeadyet seaweedbutter marycrosse lovealgorithm topicalpickuplines tehawesodotme recombobulation whatwouldjoando