2015-05-18 68 views
1

我正在製作iOS應用相關的AddressBook。 當我爲獲得具有「+」符號的電話號碼調用下面的函數時,我有時會遇到崩潰錯誤。 任何人都知道這個錯誤? 基本上,我得到componentsSeparatedByCharactersInSet函數的錯誤。如何修復componentsSeparatedByCharactersInSet的崩潰錯誤?

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

    if (dirty_number == nil) 
     return nil; 

    NSString * in_string = [email protected]"":@""; 

    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 

這是調用clearedPhoneNumber函數的代碼。

- (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; 

} 
+0

當我用上面的代碼中,我可以看到我想要什麼正確的電話號碼,我的問題是崩潰的錯誤,有時會有崩潰的bug。 –

+0

問題可能是'dirty_number'而不是字符集?使用用於獲取傳遞給'dirty_number'參數的值的代碼更新您的問題。 – rmaddy

+1

OT:歡迎來到俱樂部@rmaddy! – matt

回答