0
votes

While adding Nsdate values to NSmutablearray my app gets crashing.

 - (void)viewDidLoad {


markarry=[[NSMutableArray alloc]init];

HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate;

for (int i=0;i<[delegatObj.Datearray count]; i++) {

    NSString *Str=[delegatObj.Datearray objectAtIndex:i];

    NSLog(@"dates %@",Str);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM-dd-yyyy"];

    NSDate *dateFromString;
    dateFromString = [dateFormatter dateFromString:Str];

    NSLog(@"date type %@",dateFromString);
    [markarry addObject:dateFromString];
    [dateFromString release];
    [Str release];

}
}

If I don't release dateFromString and Str,it also gets crashing. Help me here.

This is the error Iam getting on console.

    Terminating app due to uncaught exception 'NSInvalidArgumentException', 
   reason:    -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object
   at 12' Call stack at first throw:
2

2 Answers

0
votes
reason:    -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object

dateFromString will return nil if the string isnt a valid date. So my guess is its returning nil, and then that causes the exception.

Edit: is MMMM really what you want? See: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Edit: Yes, MMMM is what you wanted. Hmmm.

0
votes

Well, as the exception you're getting clearly states, you're attempting to insert a nil object to the NSMutableArray.

So I suppose [dateFormatter dateFromString:Str] returns nil, and you shouldn't insert it to the array.