今天看到同事用了一个比较方法,先记下来:
if ([_account compare:[NSNumber numberWithInteger:_payment.integerValue]] == NSOrderedAscending){
//如果_account < [NSNumber numberWithInteger:_payment.integerValue]
}
相当于:
if (_account < [NSNumber numberWithInteger:_payment.integerValue]){ }
- (NSComparisonResult)compare:(NSNumber *)otherNumber;
结果集:(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}
原来OC提供了比较,不一定必须使用"<"、"="、">"来比较大小,
A compare: B
NSOrderedAscending 即: A<B
NSOrderedSame 即: A=B
NSOrderedDescending 即: A>B
c语言比较两个字符串是否相等:
char str1[] = "abcd";
char str2[] = "abdd";
if (strcmp(str1,str2) < 0){
//(strcmp(str1,str2) str1<str2返回负数; str1==str2 返回0; str1>str2 返回正数
}
如果用compare 来比较两个字符串
NSString *str1 = @"abcd";
NSString *str2 = @"abdd";
if ([str1 compare: str2] == NSOrderedAscending){
//str1 < str2 即str1的字母顺序在str2 之前
}