ios - NSString 对象的 length 属性有一个不正确的值

标签 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 对象时,length 属性打印出一个臃肿的数字,您可以在日志中清楚地看到它包含 10 位电话号码,仅此而已。

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

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

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

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

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

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

知道为什么 length 属性比它们应该的长吗?

感谢您的帮助。

编辑:这是我的所有代码,在我成功获得对用户地址簿的访问权之后开始,然后一直到我开始访问 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);

                }

针对 Veddermatic 的评论编辑 2:

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

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

最佳答案

长度正确,还有更多的字符没有被剥离。 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:@""];

关于ios - NSString 对象的 length 属性有一个不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221131/

相关文章:

objective-c - 在抽象 Objective-C 类中使用常量?

iphone - 在发生异步更新时更新 TableView——请告知!

javascript - Cordova/Phonegap 锁定源应用程序代码不被编辑/黑客攻击

iphone - 如何在iPhone应用程序中播放闹钟音乐?

iphone - AudioSession 实际上和 OpenAL 是一样的吗?

iphone - 将 NSString 转换为货币——完整的故事

ios - 在背景中使用与主要 UIImage 颜色相同的颜色覆盖 UIView

ios - wkWebView 加载请求失败,错误 Domain=NSURLErrorDomain Code=-999

ios - 将 scrollOffset 保存到 didEndDisplayingCell 中的 UITableView 数据源未正确出列

iphone - 通过我的 iPhone 上传图像