0

I store the data into an NSMutableArray

The issue is that the ABRecord is sometimes incomplete. For example, I am expecting the user to have the following data

First Name
Last Name
Street Address
City
State
Zip
Country

If City, State, Zip are missing then I crash. So is there a better way to check?

Here is my code

- (NSString *)getCityStateAndZip
{
    NSString* temp = [addressArray objectAtIndex:0];
    if (temp != nil) {
        NSArray *listItems = [temp componentsSeparatedByString:@","];
        NSMutableArray* tempArray = [NSMutableArray arrayWithArray:listItems];
        [tempArray removeObjectAtIndex:0];
        if ([tempArray count] > 2) {
            [tempArray removeObjectAtIndex:3];
        }
        temp = @"";
        for (NSString* element in tempArray) {
            if (element != nil) {
                //NSLog(@"%@", element);
                if ([element isEqualToString:@"(null)"]) {
                    NSLog(@"record has a null entry");
                }
                temp = [temp stringByAppendingString:element];
                temp = [temp stringByAppendingString:@", "];
            }
        }
        if ([temp length] > 1) {
            temp = [temp substringToIndex:[temp length] -2];
        }
    }
    return temp;
}

Here is a 'po tempArray' before we start removing elements.

(gdb) po tempArray
<__NSArrayM 0x5a6ee20>(
1 Main Street ,
Trenton ,
NJ ,
08601 ,
United States
)

Thanks

If you have a better way of handling this, I'd love to know.

The line which crashes the app is

[tempArray removeObjectAtIndex:3];

I know why it crashes. If the city, state, zip are missing then it can't remove the item if the array is smaller than 4.

Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177

1 Answers1

2

While you search for a better more permanent solution, and unless I am missing something in your code, wouldn't this at least stop the crash?

    if ([tempArray count] > 3) {
        [tempArray removeObjectAtIndex:3];
    }
EmphaticArmPump
  • 774
  • 4
  • 7
  • 23
  • Basically I want to handle scenario's where the count is less than 3 (incomplete address book records). In the perfect world, everything would be filled out and all fields would have a value. – Cocoa Dev Jul 14 '11 at 02:00