简而言之,我希望我的tableView
能够展示今天发生的事件(例如足球比赛)。因此,我想使用NSMutableArray
添加与addObject
匹配的词典。我已尝试使用此代码,但NSMutableArray
未显示任何内容:
self.Array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"matches" ofType:@"plist"]];
matchesToday = [[NSMutableArray alloc] init];
match = [[NSMutableDictionary alloc] init];
for (int i=0; i<[Array count]; i++) {
NSDate *datum = [[Array objectAtIndex:i] objectForKey:@"date"];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:datum];
NSDate *otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]) {
//do stuff
[matchesToday addObject:match];
NSLog(@"%i, %@", i, otherDate);
NSLog(@"Match is today");
}
else {
NSLog(@"Match is not today");
}
}
NSLog(@"%@", matchesToday);
}
plist是一系列字典,如下所示:
<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>
我做错了什么?
答案 0 :(得分:1)
您需要在日期测试if语句中添加Array中的单个项目,其中包含以下行:
[matchesToday addObject:[Array objectAtIndex:i]];
在执行此代码时,match只是一个空的可变数组。