this is a self answering question. Putting it out there to help others.
I just had a really interesting crash with my app. What I wanted to do was to define a sort comparator that would be returned a static method in the model.
So, for example if my model is Car, and I want to sort by name, I would have in Car.m
:
+ (NSComparator)nameSortAscendingComparator {
NSComparator nameSortAscendingComparator = ^NSComparisonResult(id obj1, id obj2) {
NSString *a = [(Car *)obj1 name];
NSString *b = [(Car *)obj2 name] ;
return [a compare:b] ;
};
}
The invoking code looks something like this:
NSComparator comparator = [Car nameSortAscendingComparator];
_sortedCars = [cars sortedArrayUsingComparator:comparator];
Running this gave a real red herring exception. In fact, it looked something like this:
With the breakpoint stopped on:
Thread 1, Queue : com.apple.main-thread
#0 0x0231fcbc in _objc_empty_vtable ()
#1 0x0280c70a in __56-[NSArray sortedArrayFromRange:options:usingComparator:]_block_invoke_0 ()
#2 0x0273f07d in __CFSimpleMergeSort ()
#3 0x0273f124 in __CFSimpleMergeSort ()
#4 0x0273f008 in CFSortIndexes ()
#5 0x0277ad71 in -[NSArray sortedArrayFromRange:options:usingComparator:] ()
#6 0x0279a2b5 in -[NSArray sortedArrayUsingComparator:] ()
It led me up all sorts of garden paths. Could a block not be accessed as a static variable, or via a static method? Possibly it was doing all this in the initialize
that was causing the problem?