2012-10-28 59 views
1

我試圖從地址簿中檢索聯繫人&在iOS視圖中以與iOS中的聯繫人應用程序相同的索引列表形式顯示它。我正在排序姓名上的聯繫人。如果姓氏爲空,那麼我的應用程序崩潰。這裏是我的代碼如果iOS 5的聯繫人姓氏爲空,則應用程序崩潰+

- (void)getAddressBookDataForUpdateRequest:(BOOL)isUpdateRequest { 

    if (self.peopleArray == nil) { 
     NSMutableArray *temp = [[NSMutableArray alloc]init]; 
     self.peopleArray = temp; 
    } 

    if (self.batchUserArray == nil) { 
     NSMutableArray *temp = [[NSMutableArray alloc]init]; 
     self.batchUserArray = temp; 
    } 

    row = [[NSMutableDictionary alloc] init]; 
    words = [[NSMutableArray alloc]init]; 

    ABAddressBookRef addressBook=ABAddressBookCreate(); 
    CFArrayRef cfPeople=ABAddressBookCopyArrayOfAllPeople(addressBook); 
    CFMutableArrayRef cfPeopleMutable=CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(cfPeople), cfPeople); 
    CFArraySortValues(cfPeopleMutable, CFRangeMake(0, CFArrayGetCount(cfPeopleMutable)), (CFComparatorFunction)ABPersonComparePeopleByName, (void *)ABPersonGetSortOrdering()); 
    NSArray *tempPeople=(__bridge NSArray *)cfPeopleMutable; 

    APP_DELGATE.people = [NSArray arrayWithArray:tempPeople]; 

    peopleCount = [APP_DELGATE.people count]; 

    lastCharacter = @"A"; 

    if (peopleCount >0) { 

     int noOfRec = 0; 
     for (int i=0; i<peopleCount; i++) { 

      ABRecordRef record = (__bridge ABRecordRef)[APP_DELGATE.people objectAtIndex:i]; 

      // Convert ABRecordRef to UserContactInfo object 
      UserContactInfo *user = [self getUserContactInfoFromABRecordRef:record isForUpdate:isUpdateRequest]; 

      if (user != nil) { 
       currentCharacter = [[user.lastName substringToIndex:1]uppercaseString]; 
       NSLog(@"Last: %@ :: Current:- %@",lastCharacter,currentCharacter); 

       if ([currentCharacter isEqualToString:lastCharacter]) { 
        [words addObject:user]; 

       } 
       else { 

        row = nil; 
        row = [[NSMutableDictionary alloc] init]; 

        [row setValue:lastCharacter forKey:@"sectionTitle"]; 
        [row setValue:words forKey:@"sectionRows"]; 
        [self.peopleArray addObject:row]; 
        [self.batchUserArray addObject:row]; 

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[self.batchUserArray mutableCopy],BATCH_DONE_KEY,[NSNumber numberWithBool:isUpdateRequest],@"isUpdateRequest", nil]; 
        [[NSNotificationCenter defaultCenter] postNotificationName:BATCH_DONE_NOTIFICATION object:self userInfo:dic]; 
        [self.batchUserArray removeAllObjects]; 

        words = nil; 

        words = [[NSMutableArray alloc]init]; 
        [words addObject:user]; 

        NSLog(@"ASCII Value of %@ = %d :: Last Char %@ = %d",currentCharacter,[currentCharacter characterAtIndex:0],lastCharacter,[lastCharacter characterAtIndex:0]); 
        int lastCharAsciiValue = [lastCharacter characterAtIndex:0]; 
        int currentCharAsciiValue = [currentCharacter characterAtIndex:0]; 

        while ((lastCharAsciiValue +1) < (currentCharAsciiValue)) { 

         row = nil; 
         row = [[NSMutableDictionary alloc] init]; 

         lastCharAsciiValue ++; 
         lastCharacter = [NSString stringWithFormat:@"%c",lastCharAsciiValue]; 
         [row setValue:lastCharacter forKey:@"sectionTitle"]; 
         [row setValue:[[NSMutableArray alloc]init] forKey:@"sectionRows"]; 
         [self.peopleArray addObject:row]; 
         [self.batchUserArray addObject:row]; 


        } 
       } 

       lastCharacter = currentCharacter; 
       noOfRec++; 
      } 
     } 

     // For last char "z" 
     row = nil; 
     row = [[NSMutableDictionary alloc] init]; 
     [row setValue:lastCharacter forKey:@"sectionTitle"]; 
     [row setValue:words forKey:@"sectionRows"]; 
     [self.peopleArray addObject:row]; 
     [self.batchUserArray addObject:row]; 

     NSLog(@"total rec count=%d",self.peopleArray.count); 
     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.batchUserArray,BATCH_DONE_KEY,[NSNumber numberWithBool:isUpdateRequest],@"isUpdateRequest", nil]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:BATCH_DONE_NOTIFICATION object:self userInfo:dic]; 
     [self.batchUserArray removeAllObjects]; 

     [[NSNotificationCenter defaultCenter]postNotificationName:TASK_DONE_NOTIFICATION object:self]; 

     APP_DELGATE.allUsersArray = self.peopleArray; 

    } 
    else { 
     [[NSNotificationCenter defaultCenter]postNotificationName:NO_CONTACTS_AVAILABLE_NOTIFICATION object:nil]; 
    } 
    CFRelease(addressBook); 
} 

如果姓是空的應用程序崩潰的

if ([currentCharacter isEqualToString:lastCharacter]) { 
     [words addObject:user]; 

    } 

我如何檢查是否姓是在索引列表中未署名的部分空&顯示它。 任何形式的幫助表示讚賞。在此先感謝

回答

0

請使用

如果(user.lastName!=無){

而是

如果(用戶!=無){

Full Code

- (void)getAddressBookDataForUpdateRequest:(BOOL)isUpdateRequest { 

if (self.peopleArray == nil) { 
    NSMutableArray *temp = [[NSMutableArray alloc]init]; 
    self.peopleArray = temp; 
} 

if (self.batchUserArray == nil) { 
    NSMutableArray *temp = [[NSMutableArray alloc]init]; 
    self.batchUserArray = temp; 
} 

row = [[NSMutableDictionary alloc] init]; 
words = [[NSMutableArray alloc]init]; 

ABAddressBookRef addressBook=ABAddressBookCreate(); 
CFArrayRef cfPeople=ABAddressBookCopyArrayOfAllPeople(addressBook); 
CFMutableArrayRef cfPeopleMutable=CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(cfPeople), cfPeople); 
CFArraySortValues(cfPeopleMutable, CFRangeMake(0, CFArrayGetCount(cfPeopleMutable)), (CFComparatorFunction)ABPersonComparePeopleByName, (void *)ABPersonGetSortOrdering()); 
NSArray *tempPeople=(__bridge NSArray *)cfPeopleMutable; 

APP_DELGATE.people = [NSArray arrayWithArray:tempPeople]; 

peopleCount = [APP_DELGATE.people count]; 

lastCharacter = @"A"; 

if (peopleCount >0) { 

    int noOfRec = 0; 
    for (int i=0; i<peopleCount; i++) { 

     ABRecordRef record = (__bridge ABRecordRef)[APP_DELGATE.people objectAtIndex:i]; 

     // Convert ABRecordRef to UserContactInfo object 
     UserContactInfo *user = [self getUserContactInfoFromABRecordRef:record isForUpdate:isUpdateRequest]; 

     if (user.lastName != nil) { 
      currentCharacter = [[user.lastName substringToIndex:1]uppercaseString]; 
      NSLog(@"Last: %@ :: Current:- %@",lastCharacter,currentCharacter); 

      if ([currentCharacter isEqualToString:lastCharacter]) { 
       [words addObject:user]; 

      } 
      else { 

       row = nil; 
       row = [[NSMutableDictionary alloc] init]; 

       [row setValue:lastCharacter forKey:@"sectionTitle"]; 
       [row setValue:words forKey:@"sectionRows"]; 
       [self.peopleArray addObject:row]; 
       [self.batchUserArray addObject:row]; 

       NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[self.batchUserArray mutableCopy],BATCH_DONE_KEY,[NSNumber numberWithBool:isUpdateRequest],@"isUpdateRequest", nil]; 
       [[NSNotificationCenter defaultCenter] postNotificationName:BATCH_DONE_NOTIFICATION object:self userInfo:dic]; 
       [self.batchUserArray removeAllObjects]; 

       words = nil; 

       words = [[NSMutableArray alloc]init]; 
       [words addObject:user]; 

       NSLog(@"ASCII Value of %@ = %d :: Last Char %@ = %d",currentCharacter,[currentCharacter characterAtIndex:0],lastCharacter,[lastCharacter characterAtIndex:0]); 
       int lastCharAsciiValue = [lastCharacter characterAtIndex:0]; 
       int currentCharAsciiValue = [currentCharacter characterAtIndex:0]; 

       while ((lastCharAsciiValue +1) < (currentCharAsciiValue)) { 

        row = nil; 
        row = [[NSMutableDictionary alloc] init]; 

        lastCharAsciiValue ++; 
        lastCharacter = [NSString stringWithFormat:@"%c",lastCharAsciiValue]; 
        [row setValue:lastCharacter forKey:@"sectionTitle"]; 
        [row setValue:[[NSMutableArray alloc]init] forKey:@"sectionRows"]; 
        [self.peopleArray addObject:row]; 
        [self.batchUserArray addObject:row]; 


       } 
      } 

      lastCharacter = currentCharacter; 
      noOfRec++; 
     } 
    } 

    // For last char "z" 
    row = nil; 
    row = [[NSMutableDictionary alloc] init]; 
    [row setValue:lastCharacter forKey:@"sectionTitle"]; 
    [row setValue:words forKey:@"sectionRows"]; 
    [self.peopleArray addObject:row]; 
    [self.batchUserArray addObject:row]; 

    NSLog(@"total rec count=%d",self.peopleArray.count); 
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.batchUserArray,BATCH_DONE_KEY,[NSNumber numberWithBool:isUpdateRequest],@"isUpdateRequest", nil]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:BATCH_DONE_NOTIFICATION object:self userInfo:dic]; 
    [self.batchUserArray removeAllObjects]; 

    [[NSNotificationCenter defaultCenter]postNotificationName:TASK_DONE_NOTIFICATION object:self]; 

    APP_DELGATE.allUsersArray = self.peopleArray; 

} 
else { 
    [[NSNotificationCenter defaultCenter]postNotificationName:NO_CONTACTS_AVAILABLE_NOTIFICATION object:nil]; 
} 
CFRelease(addressBook); 

}

相關問題