当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - addObject to NSMutableArray not working

问题描述:

In short, I want my tableView to show events (eg. football matches) that are happening Today. Therefore, I want to add those dictionaries that match to a NSMutableArray by use of addObject. I've tried it with this code, but the NSMutableArray does not show anything:

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);

}

The plist is an array of dictionaries that looks 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>

What am I doing wrong?

网友答案:

You need to be adding the individual item from Array inside the date test if statement with the following line:

[matchesToday addObject:[Array objectAtIndex:i]];

At the point that this code executes, match is just an empty mutable array.

分享给朋友:
您可能感兴趣的文章:
随机阅读: