【问题标题】:When using ABMultiValueCopyValueAtIndex, how to get NSString with all the special charecters removed?使用 ABMultiValueCopyValueAtIndex 时,如何在删除所有特殊字符的情况下获取 NSString?
【发布时间】:2013-09-28 07:39:08
【问题描述】:

当我使用 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,在控制台中它添加了某种“。”在空格的位置。我尝试修剪空格,但我这样做非常不成功。伙计们,你能帮我解决这个问题吗?我是不是做错了什么?

【问题讨论】:

    标签: ios objective-c ios7 xcode5 abaddressbook


    【解决方案1】:

    你可以试试

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

    从结果字符串中删除点。

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

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

    【讨论】:

    • 我也试过@NAZIK,但它不起作用。通常,如果您键入句号“。”,它将被放置在行的底部。我觉得它添加的特殊字符看起来像是一个句号,但放在了行的中间。我无法识别它是什么样的特殊字符。在控制台中,它以这种方式向我显示,但是当我将光标放在变量上进行调试时,它向我显示一个空格。奇怪的行为。
    • @N.AnantVijay,如果有帮助,您可以接受答案。
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多