如何在本机iPhone联系人应用程序中实现数字比较?

时间:2014-12-27 18:47:20

标签: ios objective-c comparison contacts phone-number

我想比较两个数字,例如: number987654。 它可以保存为+91 9876540987654。但是在searchingcomparing时,数字会完全匹配并正确显示。

现在我正在使用此代码来比较确切的数字。我该如何改进它?

// 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数字比较。 谁能给任何线索?

1 个答案:

答案 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语句中将整数与==进行比较。