I lost time tracking down a bug in a text parser I wrote due to unexpected behavior in an NSString method. My parser leverages enumerateSubstringsInRange:options:usingBlock: with the NSStringEnumerationBySentences option to tokenize sentences. Each sentence gets placed into a dictionary keyed by the sentence index.

Somehow my tests pass, but parsing fails on user inputted text. It took longer than it should have to track down the bug, but I realized the source of the issue is due to capitalization. I was able to verify the problem with following two tests. The first test passes, while the second fails miserably. I opened an issue with Apple to track the problem. I am hopeful Apple will release a fix soon. In the meantime I am going to workaround the limitation by taking the regular expression route.

Passes:

- (void)testEnumerateString1 {
   NSMutableArray *sentences = [NSMutableArray array];
   NSString *string = @"I need to do something at 1pm. This is a second sentence";
   NSRange range = NSMakeRange(0, [string length]);

   [string enumerateSubstringsInRange:range
                              options:NSStringEnumerationBySentences
                           usingBlock:^(NSString *substring,
                                        NSRange substringRange,
                                        NSRange enclosingRange,
                                        BOOL *stop) {
      [sentences addObject:substring];
   }];

   // Test passes
   XCTAssertEqual([sentences count], (NSUInteger)2, @"there should be two sentences");
}

Fails:

- (void)testEnumerateString2 {
   NSMutableArray *sentences = [NSMutableArray array];
   NSString *string = @"I need to do something at 1pm. this is a second sentence";
   NSRange range = NSMakeRange(0, [string length]);

   [string enumerateSubstringsInRange:range
                              options:NSStringEnumerationBySentences
                           usingBlock:^(NSString *substring,
                                        NSRange substringRange,
                                        NSRange enclosingRange,
                                        BOOL *stop) {
      [sentences addObject:substring];
   }];

   // Test fails
   XCTAssertEqual([sentences count], (NSUInteger)2, @"there should be two sentences");
}