Creating NSArray – EXC_BAD_ACCESS?-Collection of common programming errors
When I try to create the NSArray purchasedTimeArray, I get a crash EXC_BAD_ACCESS
NSString *blank = @"";
NSArray *purchasedTimeArray = [[NSArray alloc] initWithObjects:
timeRemainingTitle, blank, @"imagename.png", blank, description, 4, nil];
And yes, timeRemainingTitle and description still exist in memory. They are both NSStrings.
-
Use the new Objective-C syntax:
NSArray *purchasedTimeArray = @[ timeRemainingTitle, blank, @"imagename.png", blank, description, @4 ];Two benefits:
- Much easier to write.
- No need for
nilterminator and if one of the other values arenilin value, all the objects still end up in the array.
As noted in the comments, the
@4syntax translates to[NSNumber numberWithInt:4].