NSComparisonResult and NSComparator - what are they?

前端 未结 3 992
隐瞒了意图╮
隐瞒了意图╮ 2021-02-09 06:09

What is NSComparisonResult and NSComparator?

I\'ve seen one of the type definitions, something like that:

typedef NSComparisonR         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 06:22

    NSComparisonResult comparisionresult;
    NSString * alphabet1;
    NSString * alphabet2;
    
    
    // Case 1
    
    alphabet1 = @"a";
    alphabet2 = @"A";
    comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
    
    if (comparisionresult == NSOrderedSame)
        NSLog(@"a and a are same. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
    //Result: a and a are same. And the NSComparisionResult Value is 0
    
    // Case 2
    alphabet1 = @"a";
    alphabet2 = @"B";
    comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
    
    if (comparisionresult == NSOrderedAscending)
        NSLog(@"a is greater than b. And the NSComparisionResult Value is %ld \n\n", comparisionresult);
    //Result: a is greater than b. And the NSComparisionResult Value is -1
    
    // Case 3
    alphabet1 = @"B";
    alphabet2 = @"a";
    comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2];
    
    if (comparisionresult == NSOrderedDescending)
        NSLog(@"b is less than a. And the NSComparisionResult Value is %ld", comparisionresult);
    
    //Result: b is less than a. And the NSComparisionResult Value is 1
    

提交回复
热议问题