我想比较两个数字,例如: number
为987654
。
它可以保存为+91 987654
或0987654
。但是在searching
或comparing
时,数字会完全匹配并正确显示。
现在我正在使用此代码来比较确切的数字。我该如何改进它?
// Remove non numeric characters from the phone number
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSPredicate *predicate = [NSPredicate predicateWithBlock: ^(id record, NSDictionary *bindings) {
ABMultiValueRef phoneNumbers = ABRecordCopyValue( (__bridge ABRecordRef)record, kABPersonPhoneProperty);
BOOL result = NO;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
NSString *contactPhoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
contactPhoneNumber = [[contactPhoneNumber componentsSeparatedByCharactersInSet: [[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];
//Comparing the string are equal. phoneNumber is the number I am comparing to.
if ([contactPhoneNumber isEqualToString:phoneNumber]) {
result = YES;
break;
}
}
CFRelease(phoneNumbers);
return result;
}];
只有contactPhoneNumber和phoneNumber完全相同时,上面的代码才会生成YES。
不是0987654
或+91 987654
987654
时。
同样的事情也适用于WhatsApp数字比较。 谁能给任何线索?
答案 0 :(得分:0)
好吧,我知道这不是最新的代码,但是......
NSString *originalPhoneNumber = [insert phone String];
int phoneNumber = [originalPhoneNumber intValue];
NSString *phoneFormatOne;//This will be your number formatted like 0987654
NSString *phoneFormatTwo;//This will be your number formatted like +91 987654
int phoneNumberFormatOne = [phoneFormatOne intValue];
NSString *partOne = [phoneFormatTwo substringWithRange:NSMakeRange(1,3)];
NSString *partTwo = [phoneFormatTwo substringWithRange:NSMakeRange(4,10)];
int phoneNumberFormatTwo = [partOne intValue]*1000000 + [partTwo intValue];
现在你有三个整数格式,称为phoneNumber,phoneNumberFormatOne和phoneNumberFormatTwo。您现在可以在if语句中将整数与==进行比较。