簡體   English   中英

rangeOfString方法調用導致無法識別的選擇器錯誤

[英]rangeOfString method call causing unrecognized selector error

我正在訪問通訊簿聯系人,然后在所有聯系人上運行for循環。 循環的主要目的是訪問用戶通訊簿聯系人的“名字”和“電話號碼”值,並將這些值放入NSString對象中。

上面所有這些都完美地工作了。 我的問題是,如果電話號碼數據包含以下字符串,則需要執行特定的代碼: _$!<Mobile>!$_

因此,我在包含電話號碼數據的對象上設置了rangeOfString方法調用,這引起了問題。

這是我在代碼中添加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!!!

}   
}

}

謝謝您的幫助。

電話號碼不是字符串。 它被定義為multival,因為那里可以有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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM