如何从objective-c中的电话号码中删除非数字字符?

Fer*_*spo 14 objective-c phone-number

这是我第一次尝试制作ios应用程序.

我正在使用人物选择器向用户询问电话号码,但是当它使用下面的代码检索时,我的NSString *phoneapears如(0)111192222-2222.我来自巴西,这里正确的手机号码掩码是(01111)92222-2222(9是可选的,有些数字有其他人没有).如何修复这个面膜?或者完全删除它?

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
    CFIndex index = ABMultiValueGetIndexForIdentifier(multiValue, identifier);
    NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multiValue, index);
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ool 40

请参阅此答案:https://stackoverflow.com/a/6323208/60488

基本上:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
Run Code Online (Sandbox Code Playgroud)

对于您的情况,您可能希望从字符集中删除字符" - ","("和")".

  • 只是添加一些信息.在研究了这段代码后,我实际上会做`[NSCharacterSet decimalDigitCharacterSet]`因为它在API中符合我的需要! (4认同)