In my project I get a list of dates from an XML string using an NSXMLParser. The object (called DatesXML) that the parser puts the information into has a
@property (nonatomic,strong) NSDate *DATE;
@property (nonatomic,strong) NSString *NAME;
among many other properties.
My parser is going to return an array (called dateItemsFromXML) of DatesXML objects. After the parser finishes, I do
NSMutableDictionary *datesFromXMLDictionary = [[NSMutableDictionary alloc] init];
for (DatesXML *item in dateItemsFromXML)
{
[datesFromXMLDictionary setValue:item.DATE forKey:item.NAME];
}
Later, I do
if ([[datesFromXMLDictionary objectForKey:@"OneOfMyNames"] isEqualToDate:[NSDate date]])
I’m getting an “unrecognized selector” error on this if statement. I put in a breakpoint, and when I put po [datesFromXMLDictionary objectForKey:@"OneOfMyNames"] in the Console, I get (id) ... 12/3/2012.
Why is the Console telling me it is an id object instead of an NSDate object? Is that information lost when it is put into the dictionary (or anywhere else for that matter)?
As pointed out by Matthias Bauch in the comments, the root of my problem ended up being because I had an NSString in my .DATE property. When the parser was loading up my DatesXML object, it loads everything in as a string by default. As a result, it loaded my .DATE property with a string (and did so silently), even though .DATE is supposed to be an NSDate.
I put an NSDateFormatter into my parser to turn the string from the xml into an NSDate, then put that NSDate into my .DATE property. That eliminated the error I was getting and I’m getting all the behavior I expected when I try to use the .DATE property elsewhere in my code.