2013-01-04 49 views
0

我試圖在我的聯繫人列表中進行搜索,但是如果它是從應用商店剛剛執行的,或者現在正在從TestFlight進行Im測試,則此代碼會崩潰。 如果我卸載應用程序,然後點擊運行它完美。但是,從TestFlight崩潰,崩潰日誌狀態正確執行,它在我ABRecordCopyValue不同的值?

BOOL found = NO; 
NSString *name; 
int i = 0; 
NSLog(@"Hay %i", [people count]); 
while (!found && i < [people count]) { 
    ABRecordRef person = (ABRecordRef)[people objectAtIndex:i]; 
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty); 
    NSLog(@"address: %@", multi); 
    //Freshly from TestFlight this prints "address: Denis" wich is a contac, executed from Xcode it prints, address: ABMultiValueRef 0x1fb68400 with 1 value(s), so I see here's the problem 

    if([[(NSMutableString*)ABMultiValueCopyValueAtIndex(multi, 0) componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]){ 

     NSMutableString *tempPhone = [[NSMutableString alloc]initWithString:[[(NSMutableString*)ABMultiValueCopyValueAtIndex(multi, 0) componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]]; 

     NSLog(@"telf: %@", tempPhone); 

     int length = [tempPhone length] - 9; 
     NSString *tempPhone2; 
     if(length >= 0){ 
      tempPhone2 = [[NSString alloc]initWithString:[tempPhone substringFromIndex:length]]; 
     } 

     NSLog(@"el telf: %@ y nombre %@ int %i",tempPhone2, [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""], i); 

     if([[key objectForKey:@"phone"] isEqualToString:tempPhone2]){ 

      name = [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""]; 

      found = YES; 

     } 
    } 

    i++; 

} 

在此行中NSLog(@"address: %@", multi);它打印時,其從TestFlight新鮮線路出現故障「地址:丹尼斯」至極的接觸,從執行Xcode它打印,「地址:ABMultiValueRef 0x1fb68400與1值(S)...」,所以我看到這裏的問題,這種差異,我不明白爲什麼不同,你能告訴我爲什麼?

回答

1

看來您並未正確訪問地址簿數據。我明白,正確的方式會是這樣的:

ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty); 
NSString* label; 
for (int i = 0; i < ABMultiValueGetCount(multi); i++) { 
    label = (NSString*)ABMultiValueCopyLabelAtIndex(multi, i); 
    ... <DO YOUR PROCESSING on label> 
} 

希望這會有所幫助。

+0

問題是multi的值不同。所以當我嘗試訪問ABMultiValueRef的值時,它會因爲某些原因而不會返回正確的對象而崩潰。 – subharb