字符串与选项的比较在 iOS9.1 中崩溃,只有 iPhone6 和 Xcode7

string comparison with options crashes in iOS9.1 with iPhone6 only and Xcode7

该方法在 iOS9.1 中崩溃了。它是从

调用的
(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

方法是

- (void)filterContentForSearchText:(NSString*)searchText
{

    [self.searchResults removeAllObjects]; // First clear the filtered array.

    for (NSArray *contactsInSection in self.sections)
    {
        for (Contact *contact in contactsInSection)
        {
            NSArray *substringArray = [[contact displayName] componentsSeparatedByString:@", "];
            for (NSString *substring in substringArray)
            {
                NSComparisonResult result = [substring compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
                if (result == NSOrderedSame)
                {
                    [self.searchResults addObject:contact];
                    break;
                }
            }
        }
    }
}

崩溃的行是比较行:[substring compare:searchText.... ];

iOS9.1 和 iPhone6 设备出现问题。适用于 iPhone5!!!

这是编译器的截图

问题是你的范围

range:NSMakeRange(0, [searchText length])];

比接收者长:searchText = @"Po"是2个字符长,而substring = @"n"只有1个字符长。因此该方法将引发异常:

range: The range of the receiver over which to perform the comparison. The range must not exceed the bounds of the receiver.

IMPORTANT

Raises an NSRangeException if range exceeds the bounds of the receiver.

(参考:iOS API reference

也许你应该先检查一下searchText.length <= substring.length?