campare陷阱
NSString有多個compare相關方法:
- (NSComparisonResult)compare:(NSString *)string;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
NSComparisonResult 是定義的一個枚舉,定義如下:
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
其中,NSOrderedSame 表示比較的兩個字符串完全一致, 同時,在這個枚舉中,它的值是 0.
字符串比較在程序中很常見,比如:
if ([str1 compare:@"some text"] == NSOrderedSame) {
// TODO
}
else {
// TODO
}
但是,如果如上中的str1為nil,根據Objective-C的消息調用規則(方法調用),對nil發送的任何消息,得到的返回都是nil。這樣的情況下,運行時是不會像C/C++那樣,出現空指針的非法訪問而使得程序強行終止。也就是說,在Objective-C下面,即便str1為nil,也不會造成程序崩潰,而是會繼續運行。
那麼當str1為空的時候,[str1 compare:@"some text"] 消息的返回就會為nil。nil表示一個空的Objective-C對象,實際就是表示一個空指針,而它代表的值就是0,與NSOrderedSame的值相等. 如此,回到最前面的if語句,如果str1為nil,那麼整個語句的值為真。這會給程序造成非常嚴重的問題,小則邏輯錯誤,UI顯示錯誤等,大則會造成數據洩漏等等。。。所以,一旦出現這種情況,還是很嚴重的。
筆者個人建議,以上代碼至少應該寫為:
if (str1!=nil && [str1 compare:@"some text"] == NSOrderedSame) {
// TODO
}
else {
// TODO
}
OC排序代碼,直接上代碼
//數字排序
- (void)sortNumber{
NSArray *originalArray = @[@"8",@"41",@"32",@"11",@"-1"];
//block比較方法,數組中可以是NSInteger,CGFloat等(需要轉換)
NSComparator finderSort = ^(id string1,id string2){
if ([string1 integerValue] > [string2integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}else if ([string1integerValue] < [string2integerValue]){
return (NSComparisonResult)NSOrderedAscending;
}
else
return (NSComparisonResult)NSOrderedSame;
};
NSArray *resultArray = [originalArray sortedArrayUsingComparator:finderSort];
NSLog(@"排序結果:%@",resultArray);
}
//字符串排序
- (void)sortString{
// 2. 非數字型字符串(注意用compare比較要剔除空數據(nil))
NSArray *charArray =@[@"string 1",@"String 21",@"string 12",@"String 11",@"String 02"];
NSStringCompareOptions comparisonOptions =NSCaseInsensitiveSearch|NSNumericSearch|
NSWidthInsensitiveSearch|NSForcedOrderingSearch;
NSComparator sort = ^(NSString *obj1,NSString *obj2){
NSRange range = NSMakeRange(0,obj1.length);
return [obj1 compare:obj2options:comparisonOptionsrange:range];
};
NSArray *resultArray2 = [charArray sortedArrayUsingComparator:sort];
NSLog(@"字符串排序%@",resultArray2);
}
//字典排序
- (void)sortDicrionary{
NSMutableArray *array = [NSMutableArrayarrayWithObjects:
@{@"obj0":@"0"},
@{@"obj3":@"3"},
@{@"obj1":@"1"},
@{@"obj2":@"2"},
@{@"obj4":@"4"},
nil];
NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];
NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];
NSComparisonResult result = [number1compare:number2];
//return result == NSOrderedAscending; //降序
return result == NSOrderedDescending;//升序
}];
NSLog(@"OrderedDescending:%@", resultArray);
}
//自定義對象排序
- (void)sortCustomObject{
SLPerson *person1 = [[SLPersonalloc]init];
[person1 setName:@"ABCD"];
[person1 setAge:24];
SLPerson *person2 = [[SLPersonalloc]init];
[person2 setName:@"ACBD"];
[person2 setAge:22];
SLPerson *person3 = [[SLPersonalloc]init];
[person3 setName:@"ABDC"];
[person3 setAge:33];
SLPerson *person4 = [[SLPersonalloc]init];
[person4 setName:@"ACDB"];
[person4 setAge:22];
NSMutableArray *array = [NSMutableArrayarrayWithObjects:person1, person3, person4, person2,nil];
NSSortDescriptor *sortDescriptor1 = [NSSortDescriptorsortDescriptorWithKey:@"age"ascending:YES]; //先按照age排序,
NSSortDescriptor *sortDescriptor2 = [NSSortDescriptorsortDescriptorWithKey:@"name"ascending:YES]; //如果age相同,按照name排序,以此類推
NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];
for(NSInteger i =0; i < [tempArraycount]; i++){
NSLog(@"%@--------%d\n", [[tempArrayobjectAtIndex:i]name], [[tempArrayobjectAtIndex:i]age]);
}
}
代碼鏈接:http://download.csdn.net/detail/u011883764/7827311