I get that error EXC_BAD_ACESS at the following line:
NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];
Here is the for loop where the above code line is located:
for (i=0; i < count; ++i)
{
//Save the occasionS details to NSUserDefaults
NSString *titleVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionTitle",i];
NSString *dateVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionDate",i];
NSString *imageVarName = [[NSString alloc] initWithFormat:@"%@%@",@"occasionImage",i];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
title] forKey:titleVarName];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
date] forKey:dateVarName];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
imagePath] forKey:imageVarName];
//release
[titleVarName release];
[dateVarName release];
[imageVarName release];
[self dismissModalViewControllerAnimated:YES];
}
Isn’t ok to alloc objects and release them inside a for loop?
You need to use
%dor%ispecifier instead of%@to specify an integer. If%@is used with int then it will try to access the object at the address specified by the int. For example, if the value ofiis one then it is trying to access the object at address one which will cause a bad access.And also you don’t need
allocandreleasehere, though that is not the reason of bad access. You can use a convenience constructor.Do the same for
dateVarNameandimageVarNametoo.