1

I'm trying to pull NSDates from my plist, in order to create Datecomponents, but trying to do so give me problems.

This is some of the code I'm using:

self.Array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"matches" ofType:@"plist"]];

NSDateComponents *todayComp = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
NSDateComponents *eventDateComp = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[Array valueForKey:@"date"]];

I'm getting the todayComp's correctly, but the eventDateComp's, which I'm trying to get frm the plist, gives an error.

The plist is an array of dictionaries, like this:

<array>
<dict>
    <key>date</key>
    <date>2011-12-13T00:00:00Z</date>
    <key>titel</key>
    <string>Tilburg - Oss</string>
</dict>
<dict>
    <key>date</key>
    <date>2011-12-13T00:00:00Z</date>
    <key>titel</key>
    <string>Amsterdam - Roosendaal</string>
</dict>
</array>

NSLog says something like this...

-[__NSArrayI timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x6a8d050

...which probably holds that I'm not getting the NSDates from the plist in the right way, but I have tried some different things, but have no clue how to do it right. Help is greatly appreciated.

Monolo
  • 18,205
  • 17
  • 69
  • 103
user1048042
  • 115
  • 1
  • 1
  • 8

1 Answers1

1
[Array valueForKey:@"date"]

You are using valueforKey: method on NSArray. 1) use objectAtIndex: to get an object from array; 2) Difference between objectForKey and valueForKey?

NSDate *eventDate = [[Array objectAtIndex:0] objectForKey:@"date"];
NSDateComponents *eventDateComp = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:eventDate];
Community
  • 1
  • 1
Kreiri
  • 7,840
  • 5
  • 30
  • 36
  • Tried to implement the code and works, but it only gets the date from the first dictionary (due to objectAtIndex being 0). How can I go through all the dictionaries and thus get all the dates? – user1048042 Dec 13 '11 at 13:37
  • http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Enumerators.html#//apple_ref/doc/uid/20000135-BBCFABCB – Kreiri Dec 13 '11 at 14:42