NSString对象的length属性具有不正确的值

时间:2014-01-19 18:56:02

标签: ios iphone objective-c nsstring string-length

我正在访问iPhone上的地址簿,并将电话号码存储在名为numberValues的NSString对象中。最初这些电话号码被大量额外的垃圾包围,如引号,加号,括号,空格等。所以我有所有这些rangeOfString和stringByReplacingOccurencesOf方法调用设置来摆脱所有额外的代码。

删除所有额外的垃圾后,我留下了10位数的电话号码,这就是全部。因此,如果联系人有1个电话号码,那么我的numberValues对象的长度属性应为10.如果联系人有2个电话号码,那么我的numberValues对象的长度属性应为20。

这就是奇怪的。当我在一个电话号码的联系人上使用NSLog numberValues.length时,它会向控制台输出12的长度。如果我在有两个电话号码的联系人上做同样的事情,它会在控制台上打印23的长度。

我不明白为什么当我花时间删除所有额外的垃圾(包括空格)时,长度属性是用臃肿的数字打印,当我NSLog的numberValues对象时,你可以在日志中清楚地看到它包含10位数的电话号码,仅此而已。

当我执行NSLog(@"%@", numberValues)时,以下是如何将1个电话号码的联系人打印到日志中:

2014-01-19 10:36:54.912 PhotoTest1[2658:60b] 
9495553119

请记住,当我NSLog(@"%d", numberValues.length)时,它会打印出来。

当我执行NSLog(@"%@", numberValues)时,以下是如何将2个电话号码的联系人打印到日志中:

014-01-19 10:36:54.737 PhotoTest1[2658:60b] 
7142973557
7149557735

请记住,当我NSLog(@"%d", numberValues.length)时,它会打印出23。

为什么长度属性比它们应该长的任何想法?

感谢您的帮助。

编辑:以下是我成功访问用户地址簿之后开始的所有代码,然后一直到我开始访问numberValues对象的length属性的地方:

int z = allContacts.count - 1;


    for (int i = 0; i < z; i++)


    {

        NSArray *allContacts = (__bridge_transfer NSArray
                                *)ABAddressBookCopyArrayOfAllPeople(m_addressbook);



        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];


        NSString *firstName = (__bridge_transfer NSString
                               *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

        ABMultiValueRef *phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);



        NSMutableArray *numbers = [NSMutableArray array];

        //NSLog(@"The count: %ld", ABMultiValueGetCount(phoneNumber));

        NSString *number = (__bridge_transfer NSString*)ABMultiValueCopyArrayOfAllValues(phoneNumber);



        [numbers addObject:number];

        NSString *numberValues = [[NSString alloc]initWithFormat:@"%@", number];


        NSLog(@"Here are the numbers for the contact named %@: %@", firstName, numberValues);



        if([numberValues rangeOfString:@"+"].location == NSNotFound) {

            NSLog(@"Phone Number does not contain a plus sign.");

            } else {



               numberValues =  [numberValues stringByReplacingOccurrencesOfString:@"+1" withString:@""];

                NSLog(@"This new number value should not have a + anymore: %@", numberValues);



            }

        if([numberValues rangeOfString:@" ("].location == NSNotFound) {

            NSLog(@"No phone numbers contain blank space with start of parentheses");



        } else {

            NSLog(@"Phone number(s) do contain blank space with start of parentheses");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@" (" withString:@""];

            NSLog(@"The new number values should not contain a blank space with the start of parentheses: %@", numberValues);


        }


        if([numberValues rangeOfString:@") "].location == NSNotFound) {

            NSLog(@"Phone number(s) do not contain ) and a blank space");



        } else {


            NSLog(@"Phone numbers do contain ) and a blank space");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@") " withString:@""];

            NSLog(@"These new phone number values should not have ) with a blank space after it: %@", numberValues);


        }

        if([numberValues rangeOfString:@"-"].location == NSNotFound) {

            NSLog(@"No numbers contain a dash");



        } else {


            NSLog(@"Number(s) does contain a dash");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@"-" withString:@""];

            NSLog(@"The new number values should not have any dashes: %@", numberValues);


        }

        if([numberValues rangeOfString:@"("].location == NSNotFound) {

            NSLog(@"Number(s) do not contain a (");


        } else {


            NSLog(@"Number(s) contains a (");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@"(" withString:@""];

            NSLog(@"The new number(s) should not have any ( in them: %@", numberValues);



        }

        if ([numberValues rangeOfString:@"\""].location == NSNotFound) {


            NSLog(@"Number does not contain any quotations");

        } else {


            NSLog(@"Number(s) do contain quotations");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@"\"" withString:@""];

            NSLog(@"Numbers should not have any quotations: %@", numberValues);
        }

        if([numberValues rangeOfString:@")"].location == NSNotFound) {

            NSLog(@"The final ) has been deleted");


        } else {

            NSLog(@"Need to delete the final )");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@")" withString:@""];

            NSLog(@"Final value(s) should not have a trailing ) at the end: %@", numberValues);



        }

        if([numberValues rangeOfString:@","].location == NSNotFound) {

            NSLog(@"The final , has been deleted");


        } else {

            NSLog(@"Need to delete the final ,");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@"," withString:@""];

            NSLog(@"%@", numberValues);

            NSLog(@"Final value(s) should not have a trailing , at the end: %lu", (unsigned long)numberValues.length);



        }

        if([numberValues rangeOfString:@" "].location == NSNotFound) {

            NSLog(@"No blank spaces.");


        } else {

            NSLog(@"There are blank spaces that need to be deleted");

            numberValues = [numberValues stringByReplacingOccurrencesOfString:@" " withString:@""];

            NSLog(@"%@", numberValues);



        }

        NSLog(@"RIGHT BEFORE THE NEW IF STATEMENT");
        NSLog(@"%d", numberValues.length);

        if(numberValues.length < 11) {

            NSLog(@"RUNNING PFQUERY");


            PFQuery *query = [PFQuery queryWithClassName:@"_User"];



            [query whereKey:@"username" equalTo:numberValues];


            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                if (!error) {


                    NSLog(@"Here are the objects that have been returned from Parse: %@", objects);


                } else {

                    NSLog(@"CHARACTERS: %d", numberValues.length);

                    int howManyNumbers = numberValues.length / 10;

                    NSLog(@"%d", howManyNumbers);

                }

编辑2对于VEDDERMATIC的评论:

我刚刚做了NSLog(@"###%@###", numberValues);,这就是它打印到日志的方式:

2014-01-19 11:06:00.529 PhotoTest1[2678:60b] ###
3097679973
###

1 个答案:

答案 0 :(得分:2)

长度正确,有更多字符表示您没有剥离。 NSString支持超过一百万个字符,您无法检查所有字符。

相反,您应该使用RegEx删除不是数字的所有内容。

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9]" options:0 error:nil];

NSString *phoneNumber = [regex stringByReplacingMatchesInString:phoneNumber options:0 range:NSMakeRange(0, phoneNumber.length) withTemplate:@""];