Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7联系人号码空格不是空格_Ios_Ios6_Ios7 - Fatal编程技术网

iOS 7联系人号码空格不是空格

iOS 7联系人号码空格不是空格,ios,ios6,ios7,Ios,Ios6,Ios7,在我的应用程序中,我试图检索联系人的号码列表,并尝试对其进行操作。我意识到,每当我添加新联系人时(更新到iOS 7后),新联系人的格式都会改变,因为新添加的号码中有空格 使用普通替换方法不会删除空格 Boolean shouldAddPlus = NO; if([[phoneNumber substringToIndex:1] isEqualToString:@"+"]) { shouldAddPlus = YES; } phoneNumber = [[

在我的应用程序中,我试图检索联系人的号码列表,并尝试对其进行操作。我意识到,每当我添加新联系人时(更新到iOS 7后),新联系人的格式都会改变,因为新添加的号码中有空格

使用普通替换方法不会删除空格

 Boolean shouldAddPlus = NO;

 if([[phoneNumber substringToIndex:1] isEqualToString:@"+"])
    {
        shouldAddPlus = YES;
    }

 phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

 if(shouldAddPlus)
    phoneNumber = [base stringByAppendingString:phoneNumber];
这些真的是空间还是什么?我的目标是找回“空间”免费号码

例如,如果号码是
1 818 323 323 323
,我想得到
1818323323

试试这个:

NSString *cleaned = [[phoneNr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
        NSString* stringFormatted = [phoneNumber stringByReplacingOccurrencesOfString:@"\u00a0" withString:@""];

这适用于任何类型的空间()。这似乎有点低效,但对于电话号码来说,这应该没问题。

一些iOS7电话号码是用不间断空格编码的。试试这个:

        NSString* stringFormatted = [phoneNumber stringByReplacingOccurrencesOfString:@"\u00a0" withString:@""];

在经历了太多的字符串清理之后,我终于在直接从地址簿打印了CFStringRef之后找到了答案。下面是幕后发生的事情

  • 在iOS7中添加联系人,苹果存储以下内容:(555).555-5555(其中.实际上是U00A0或
  • 我的应用程序将联系人的信息从通讯簿复制到CFStringRef中(当NSLogged the.shows时)
  • CFStringRef被强制转换为NSString(NSLog现在显示555\U00a0555-5555)
  • 要删除\U00A0,我尝试了此线程中的3个答案和[NSCharacterSet CharacterSet WithRange:(160,1)],但均无效。最终起作用的是这一行代码:

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

    //其中@“.”是通过键入Option+Spacebar创建的

    。这段代码为我做到了这一点:

    phoneNumberString = [[phoneNumberString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
    
    这会去掉所有不是0-9的数字

    Swift 4.1:

    phoneNumberString = phoneNumberString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
    

    我对海报的答案做了一个小小的调整,以防有人想在数字的开头保持+这个数字

    我做了这个小小的调整,如果你想在删除空格后保持加号的话

     Boolean shouldAddPlus = NO;
    
     if([[phoneNumber substringToIndex:1] isEqualToString:@"+"])
        {
            shouldAddPlus = YES;
        }
    
     phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
    
     if(shouldAddPlus)
        phoneNumber = [base stringByAppendingString:phoneNumber];
    

    我在应用程序中使用的最干净的解决方案是:

    NSMutableCharacterSet *phoneNubmerCharacterSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"+"];
    [phoneNubmerCharacterSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
    NSString* phoneString = [[phoneString componentsSeparatedByCharactersInSet:[phoneNubmerCharacterSet invertedSet]] componentsJoinedByString:@""];
    

    无“如果”逻辑,保留+的数字,删除所有不需要的随机字符

    正确的解决方案是用有效的空格替换出现的字符

    NSString *clean = [dirty stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];
    
    这样你就不会失去空间,用户就能看到其他应用中的内容。

    另一个(非常灵活的)选择是使用正则表达式。这允许您保留+或任何其他要保留的字符

    let numberFromAddressBook = "+1 818 323 323 323"
    
    let cleanNumber = numberFromAddressBook.stringByReplacingOccurrencesOfString("[^0-9+]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range:nil)
    
    "+1818323323323"
    

    什么是“普通替换方法”@mrueg可能是标准的NSString StringByReplace*方法对我不起作用:[我几乎可以肯定,在尝试替换@“@”(alt空格)和@“\u00a0”之后,这种方法会起作用,但这种方法对我不起作用对我起作用关于通讯录电话号码的\U00a0问题为了在我的Localizable.strings文件中起作用,我必须使用所有大写:
    \U00a0
    这种方法对我不起作用这对我起作用,但是如果你有兴趣保留,这将从号码中删除+此外,您还需要对其应用一些逻辑。太棒了!这就像一个符咒。非常感谢:d这非常有效,因为可能会有许多不需要的字符,如\u0000等。