使用ABMultiValueCopyValueAtIndex时,如何删除所有特殊字符的NSString?

时间:2013-09-28 07:39:09

标签: ios objective-c ios7 xcode5 abaddressbook

当我使用ABpeoplePickerNavigationController时,调用下面的委托时, 我正在尝试执行以下操作:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    if(property == kABPersonPhoneProperty || property == kABPersonEmailProperty)
    {
        NSString *name = (__bridge NSString *)ABRecordCopyCompositeName(person);
        ABMutableMultiValueRef multi = (ABMultiValueRef)ABRecordCopyValue(person, property);
        NSString *number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, identifier);
        if(![VZMUtils validateEmail:number])
            number = [VZMUtils formatIdentificationNumber:number];
        number = [number stringByReplacingOccurrencesOfString:@"." withString:@""];
        ALog(@"number %@ and name %@",number,name);
        [self.popoverDelegate setDataWithName:name andDetail:number];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid type selected." message:@"Please select either a phone number or email address to add as a recipient" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}

我得到的变量号是.925.3248930,在控制台中它添加了某种“。”在空白的位置。我尝试修剪空白仍然是我这样做非常不成功。伙计们,你能帮助我吗?我做错了吗?

1 个答案:

答案 0 :(得分:7)

你可以尝试

number = [number stringByReplacingOccurrencesOfString:@"." withString:@""];

从结果字符串中删除点。

或者如果有其他特殊字符可能使用以下代码

NSCharacterSet *onlyAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
number = [[number componentsSeparatedByCharactersInSet:onlyAllowedChars] componentsJoinedByString:@""];