我收到一个奇怪的排序错误。我在一个名为 wordArrayDuplicates 的数组中有一个范围列表。我需要根据范围位置对这个数组进行排序,所以我将字符串转换为 NSNumber 但它不会比较。
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString sortedArrayUsingComparator:]:
- (NSArray *)sortedArray {
if (!sortedArray) {
sortedArray = [[NSArray alloc]init];
}
return [wordArrayDuplicates sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2){
NSNumber * lastName1 = [NSNumber numberWithInt:NSRangeFromString(obj1).location];
NSNumber * lastName2 = [NSNumber numberWithInt:NSRangeFromString(obj2).location];
return [lastName1 compare:lastName2]; }];
}
-(void)beginGoingThroughDuplicatesAtWordIndex:{
if (index == 1) {[self loadDuplicates];}
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blackColor].CGColor
range:NSMakeRange(0, string.length)];
BOOL shouldContinue = YES;
while (shouldContinue == YES && index<[[self sortedArray] count]) {
if (![[tv.text substringWithRange:NSRangeFromString([[self sortedArray] objectAtIndex:index-1])] isEqualToString:[tv.text substringWithRange:NSRangeFromString([[self sortedArray] objectAtIndex:index])]]) {
shouldContinue = NO;
}
[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor redColor].CGColor
range:NSRangeFromString([sortedArray objectAtIndex:index-1])];
index++;
}
textlayer.string = string;
}