2014-12-27 35 views
0

我想比較兩個數字,例如, number987654。 它可以保存爲+91 9876540987654。但searchingcomparing,數字完全匹配並正確顯示。如何在本地iPhone通訊錄應用程序中實現數字比較?

現在我正在使用此代碼來比較確切的數字。我如何增強它?

// 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; 
}]; 

上述代碼結果爲是,只有當聯繫人的電話號碼和電話號碼完全相同時。 不是當0987654+91 987654987654

同樣的事情也可以在WhatsApp號碼比較中起作用。 任何人都可以提供任何線索?

+1

請提供更多關於兩個數字相等的信息。我不明白你的單個例子如何決定兩個給定的電話號碼是否相同。刪除前導零,刪除國家/地區代碼...請包括完整列表中需要採取的步驟來規範化數字。 – bdesham 2014-12-27 20:06:54

+0

Amol,您可能需要考慮使用Google電話號碼庫https://github.com/iziz/libPhoneNumber-iOS,它非常擅長解析,格式化和比較電話號碼。這個庫應該處理很多常見和不明確的情況。 – 2014-12-28 03:15:15

回答

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語句中將整數與==相比較。

相關問題