如何修复componentsSeparatedByCharactersInSet上的崩溃错误?

时间:2015-05-18 14:24:17

标签: ios nsarray addressbook nscharacterset

我正在制作与IOS app相关的AddressBook。 当我打电话给下面的功能获取电话号码" +"符号,我有时会遇到崩溃错误。 有人知道这个错误吗? 基本上我在componentsSeparatedByCharactersInSet函数上遇到错误。

- (NSString* ) clearedPhoneNumber:(NSString* ) dirty_number withPlus:(BOOL)plus {

    if (dirty_number == nil)
        return nil;

    NSString * in_string = plus?@"+0123456789":@"0123456789";

    NSString * phone_str = [[dirty_number componentsSeparatedByCharactersInSet:
                         [[NSCharacterSet characterSetWithCharactersInString:in_string]
                          invertedSet]] 
                        componentsJoinedByString:@""];

    // Get last 10 digits
    NSRange range = NSMakeRange(0, 0);
    if ([phone_str length] > MAX_PHONE_LENGTH) {
        range.location = 0;
        range.length = [phone_str length] - MAX_PHONE_LENGTH;
    }

   return [phone_str stringByReplacingCharactersInRange:range withString:@""];
}

此处还有崩溃报告。

Thread 3 Crashed:
0   libsystem_kernel.dylib              0x1956a3270 __pthread_kill (in libsystem_kernel.dylib) + 8
1   libsystem_c.dylib                   0x19561ab18 abort (in libsystem_c.dylib) + 112
2   libsystem_malloc.dylib              0x1956de3e4 _nano_malloc_check_clear (in libsystem_malloc.dylib) + 0
3   libsystem_malloc.dylib              0x1956de550 _nano_malloc_check_clear (in libsystem_malloc.dylib) + 364
4   libsystem_malloc.dylib              0x1956dd0dc nano_calloc (in libsystem_malloc.dylib) + 80
5   libsystem_malloc.dylib              0x1956d195c malloc_zone_calloc (in libsystem_malloc.dylib) + 124
6   libsystem_malloc.dylib              0x1956d18bc calloc (in libsystem_malloc.dylib) + 64
7   libobjc.A.dylib                     0x194f17c24 class_createInstance (in libobjc.A.dylib) + 80
8   CoreFoundation                      0x1836e8004 __CFAllocateObject2 (in CoreFoundation) + 24
9   CoreFoundation                      0x1835c9eac +[__NSArrayI __new:::] (in CoreFoundation) + 40
10  CoreFoundation                      0x1835c9da0 +[NSArray arrayWithObject:] (in CoreFoundation) + 56
11  Foundation                          0x1845dd528 -[NSString componentsSeparatedByCharactersInSet:] (in Foundation) + 456
12  MyApp                               0x1001aad80 0x1000e4000 + 814464
13  MyApp                               0x1001ab434 0x1000e4000 + 816180
14  AddressBook                         0x182867198 __37-[ABTCC accessRequestWithCompletion:]_block_invoke (in AddressBook) + 48
15  libdispatch.dylib                   0x19555d994 _dispatch_call_block_and_release (in libdispatch.dylib) + 24
16  libdispatch.dylib                   0x19555d954 _dispatch_client_callout (in libdispatch.dylib) + 16
17  libdispatch.dylib                   0x19556a780 _dispatch_root_queue_drain (in libdispatch.dylib) + 1848
18  libdispatch.dylib                   0x19556bc4c _dispatch_worker_thread3 (in libdispatch.dylib) + 108
19  libsystem_pthread.dylib             0x19573d22c _pthread_wqthread (in libsystem_pthread.dylib) + 816
20  libsystem_pthread.dylib             0x19573cef0 start_wqthread (in libsystem_pthread.dylib) + 4

这是调用了clearPhoneNumber函数的代码。

- (NSMutableArray *) Get_Phones_By_Person:(ABRecordRef)person localized:(BOOL)isLocalized {

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

if (phones == nil)
    return nil;

NSMutableArray * all_phones = [NSMutableArray array];

for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
    NSString*  stripped_phone = [self clearedPhoneNumber:(__bridge NSString *)phoneNumberRef withPlus:YES];

    stripped_phone = [self getInternationalPhoneNumber:stripped_phone];
    if (stripped_phone != nil) {
        if (!isLocalized)
            [all_phones addObject:stripped_phone];
        else {
            CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
            NSMutableDictionary * dict;
            if (locLabel != nil) {
                NSString* phoneLabel =(NSString*) CFBridgingRelease(ABAddressBookCopyLocalizedLabel(locLabel));
                dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:(__bridge NSString *)phoneNumberRef, @"number", phoneLabel, @"name", nil];
                CFRelease(locLabel);
            }
            else {
                dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:(__bridge NSString *)phoneNumberRef, @"number", nil];
            }

            [all_phones addObject:dict];
        }
    }

    CFRelease(phoneNumberRef);
}     

CFRelease(phones);

return all_phones;

}

1 个答案:

答案 0 :(得分:0)

我在两个模块中使用了地址簿。 我已从linphone模块中删除了地址簿。 所以消失了这个崩溃错误。 请参阅以下链接。

Crash report when user accesses the address book

谢谢