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(@"%@",开发者_StackOverflow社区 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.
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];
}
精彩评论