1

我正在尝试从通讯录中检索联系人并将其显示在表格视图中,索引列表与 iOS 中的联系人应用程序相同。我正在按姓氏对联系人进行排序。如果姓氏为空,则我的应用程序崩溃。这是我的代码

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

    }

如何检查姓氏是否为空并将其显示在索引列表的未命名部分中。任何形式的帮助表示赞赏。提前致谢

4

1 回答 1

0

请用

if (user.lastName != nil) {

相当

如果(用户!= 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 回答