A bug in the book's code


#1

double tap removes both the (red) lines in progress and the (black) finished lines. If I actually try double tapping while i am drawing lines (so that some of the lines are red) the app crashes.

//
// main.m

#import <UIKit/UIKit.h>
#import “AppDelegate.h”

int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //thread 1 signal SIGABRT
}
}

2015-02-12 20:39:32.200 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:32.225 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:32.249 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:32.483 TouchTracker[1297:1190266] Recognized Double Tap
2015-02-12 20:39:42.028 TouchTracker[1297:1190266] ending DoubleTap
2015-02-12 20:39:42.035 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.036 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.037 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.038 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.039 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.040 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.041 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.041 TouchTracker[1297:1190266] touchesMoved:withEvent:
2015-02-12 20:39:42.042 TouchTracker[1297:1190266] touchesEnded:withEvent:
2015-02-12 20:39:42.043 TouchTracker[1297:1190266] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil’
*** First throw call stack:
(0x18742659c 0x1980800e4 0x18730f290 0x1000a969c 0x18bc08a40 0x18bc01f94 0x18bbd568c 0x18be7460c 0x18bbd3bf4 0x1873de9ec 0x1873ddc90 0x1873dbd40 0x1873090a4 0x19048f5a4 0x18bc3aaa4 0x1000a81f4 0x1986eea08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

when i double tap with only finished lines on the screen it does not crash. It definitely gets past both of the delete operations. I’m trying to just double tap but there are extra events as you can see above.

-(void) doubleTap:(UIGestureRecognizer *) gr {
NSLog(@“Recognized Double Tap”);
[self.linesInProgress removeAllObjects];
[self.finishedLines removeAllObjects];
[self setNeedsDisplay];
NSLog(@“ending DoubleTap”);
}

If I alter touchesEnded to :

-(void) touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event{
NSLog(@"%@", NSStringFromSelector(_cmd));
for (UITouch *t in touches) {
NSValue *key = [NSValue valueWithNonretainedObject:t];
MHELine *line = self.linesInProgress[key]; //get line from linesInProgress dictionary
if (line) {
[self.finishedLines addObject:line]; //move line into finishedLines Array
[self.linesInProgress removeObjectForKey:key]; //and delete from linesInProgress
}

}

the crash goes away.