0
我想比較兩個數字,例如, number
是987654
。 它可以保存爲+91 987654
或0987654
。但searching
或comparing
,數字完全匹配並正確顯示。如何在本地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 987654
與987654
。
同樣的事情也可以在WhatsApp號碼比較中起作用。 任何人都可以提供任何線索?
請提供更多關於兩個數字相等的信息。我不明白你的單個例子如何決定兩個給定的電話號碼是否相同。刪除前導零,刪除國家/地區代碼...請包括完整列表中需要採取的步驟來規範化數字。 – bdesham 2014-12-27 20:06:54
Amol,您可能需要考慮使用Google電話號碼庫https://github.com/iziz/libPhoneNumber-iOS,它非常擅長解析,格式化和比較電話號碼。這個庫應該處理很多常見和不明確的情況。 – 2014-12-28 03:15:15