1

アドレス帳から連絡先を取得して、iOS の連絡先アプリと同じインデックス付きリストを使用してテーブル ビューに表示しようとしています。LAST NAME で連絡先を並べ替えています。姓が空の場合、アプリがクラッシュします。これが私のコードです

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

LAST NAME が EMPTY の場合、アプリがクラッシュします

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

    }

姓が空であるかどうかを確認し、インデックス付きリストの UNNAMED セクションに表示するにはどうすればよいですか。どんな種類の助けも大歓迎です。前もって感謝します

4

1 に答える 1

0

使ってください

if (user.lastName != nil) {

それよりも

if (ユーザー!= nil) {

完全なコード

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

}

于 2012-10-28T08:11:08.707 に答える