【问题标题】:rangeOfString method call causing unrecognized selector errorrangeOfString 方法调用导致无法识别的选择器错误
【发布时间】:2014-01-18 20:32:19
【问题描述】:

我正在访问通讯录联系人,然后对所有联系人运行 for 循环。该循环的主要目的是访问用户通讯录联系人的 First Name 和 Phone Number 值,并将这些值放入 NSString 对象中。

以上所有功能都完美无缺。我的问题是,如果电话号码数据包含这个字符串,我需要执行特定的代码:_$!<Mobile>!$_

所以我在包含电话号码数据的对象上设置了一个rangeOfString 方法调用,这会导致问题。

这是在我将rangeOfString 方法调用添加到我的代码后打印到控制台的内容:

2014-01-18 12:10:49.190 PhotoTest1[1244:60b] -[__NSCFType rangeOfString:]: unrecognized     selector sent to instance 0x14e9ac90
2014-01-18 12:10:49.193 PhotoTest1[1244:60b] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '-[__NSCFType rangeOfString:]: unrecognized selector    sent to instance 0x14e9ac90'
*** First throw call stack:
(0x2f8f6f4b 0x39d376af 0x2f8fa8e7 0x2f8f91cb 0x2f8fa478 0x55e29 0x3206e37b 0x3206e139     0x32117e27 0x3215449b 0x32152dd3 0x32151e25 0x3232dcb3 0x3209e713 0x3209e6b3 0x3209e691    0x3208a11f 0x3209e107 0x3209ddd9 0x32098e65 0x3206e79d 0x3206cfa3 0x2f8c2183 0x2f8c1653 0x2f8bfe47 0x2f82ac27 0x2f82aa0b 0x34551283 0x320ce049 0x54495 0x3a23fab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我所有从 for 循环开始的代码。错误一到达“if([phoneNumber rangeOfString:@"+"].location == NSNotFound)”就会打印到控制台:

for (i = 0; i < [allContacts count]; i++)
    {


        Person *person = [[Person alloc] init];

        ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
        NSString *firstName = (__bridge_transfer NSString
                                *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *phoneNumber = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);


        person.firstName = firstName;

        person.phoneNumber = phoneNumber;

        NSLog(@"%@", phoneNumber);

        //MOBILE PHONE PSEUDOCODE STARTS HERE

        // If the value of phoneNumber contains the text “_$!<Mobile>!$_” , then we want to grab a substring out of it.

        if([phoneNumber rangeOfString:@"_$!<Mobile>!$_"].location == NSNotFound) {

            NSLog(@"Not a mobile phone number");

        } else {

            NSLog(@"It is a mobile phone number");

            // If the value of phoneNumber contains a “+”, then grab the substring that starts with the + and then the next additional 10 spaces and place the substring in the mobilePhoneNumber object. (Which will grab the + sign and the 10 digit phone number. )

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


                NSLog(@"Phone number does not start with a +");


            } else {

                NSLog(@"Phone number does start with a +");

                NSString *subString = [phoneNumber substringWithRange: NSMakeRange(0, [phoneNumber rangeOfString: @"+"].location)];

                NSLog(@"%@", subString);

            }

        }

        //PFQUERY CODE TESTING!!!

}   
}

}

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c for-loop unrecognized-selector nsrange


    【解决方案1】:

    电话号码不是字符串。它被定义为一个多值,因为那里可以有 0..x 个数字。

    你不能只把它当作一个字符串。你需要通过multival来枚举\

    例如获得任何类型的第一个

        ABMultiValueRef phoneEntries = ABRecordCopyValue(person, kABPersonPhoneProperty);       
        NSMutableArray *numbers = [NSMutableArray array)];
        for(int i = 0; i < ABMultiValueGetCount(phoneEntries); i++) {
             NSString *number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneEntries, i))];
             // Do what you want with the number
             [numbers addObject:number];
        }
        CFRelease(phoneEntries);
    

    【讨论】:

    • 明白了。我的主要目标是提取手机号码数据并摆脱存储的所有其他垃圾。我可以通过枚举你所说的 multival 来做到这一点,如果可以的话,你能给我一个代码示例或指向一些文档吗?
    • 所以如果我理解正确......通过使用上面的代码,我将能够完全按照我之前尝试做的事情并通过使用 rangeOfString 来做到这一点,因为你现在让我使用 ABMultiValueGetCount和 ABMultiValueCopyValueAtIndex 而不是使用 count 和 ABRecordCopyValue?
    • 我仍然不明白为什么当我实例化一个 NSString 对象并能够将数据放在那里时,我们不能将 rangeOfString 与我的原始代码一起使用。如果我可以首先将数据放在 NSString 中,那么为什么我不能使用 rangeOfString?这种情况与我在 NSString 对象上使用 rangeOfString 的所有其他时间有什么区别?
    • 我认为这是一个字符串,因为这个语句: NSString *phoneNumber = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
    • 只是因为你说它应该是。将指针类型视为仅仅是一个标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    相关资源
    最近更新 更多