0
votes

I have built one 'deep' NSMutableDictionary that contains parsed XML data along with other relevant information that I can pass from one view controller to another. The NSMutableDictionary contains mostly NSStrings but also another NSMutableDictionary more NSStrings and finally followed even deeper with an NSMutableArray of custom objects (it's for a calendar).

Now, because it is a calendar, there are obviously dates involved. The XML data that I receive and parse using NSXMLParser returns strings, so it was on me to convert the day's date to usable numbers. The date in XML comes in with the following format: "MM.DD" I created the following method to do so:

- (void)createDateCodesWithString:(NSString *)string 
{ 
   NSInteger monthCode;
   NSInteger dayCode;
   ....
   NSArray *dates = [string componentsSeparatedByString:@"."];
   monthCode = [[dates objectAtIndex:0] integerValue];
   dayCode = [[dates objectAtIndex:1] integerValue];
   ....
   shortDay = [NSNumber numberWithInt:dayCode];
}

'shortDay' is a NSNumber* and an ivar and set as a property (nonatomic, retain) for the custom object that I have created. When I run NSLog commands in the console, it appears that 'shortDay' and other data has been stored successfully in the deep NSMutableDictionary. I run in to problems, however, when I try to access the data again. When I access a NSString* stored ivar, things work OK, but when I attempt to access the NSNumber* I am given the error EXC_BAD_ACCESS with either code 1 or code 2. This is how I try to call upon the NSNumber*

   NSNumber *number = day.shortDay;
   return [number stringValue];

Might the problem be because the NSArray *dates strips the string into month and day strings and the day string, being two characters long, may contain a '0' before, say, a '6' if the day is the 6th of the month? Any advice?

I am happy to post more code if needed.

1
Check the NSNumber class reference how to create such an object. Assigning an integer will not work. Didn't you get any warnings?ott--

1 Answers

1
votes

It's possible that the memory for shortDay is being cleaned up before the next time you try to access it. When assigning it, try this instead:

shortDay = [[NSNumber numberWithInt:dayCode] retain];

to increase the reference count (AKA take ownership of the object) to avoid the memory being deallocated too early.

If this resolves the problem, you will then need to call [shortDay release] in the dealloc method of your class, such that the memory for it will be properly deallocated at the right time.