繁体   English   中英

NSMutableArray的addObject不起作用

[英]addObject to NSMutableArray not working

简而言之,我希望tableView显示今天发生的事件(例如,足球比赛)。 因此,我想通过addObject添加与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>

我究竟做错了什么?

您需要在日期测试if语句中的以下行中添加Array中的单个项:

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

在执行此代码时,match只是一个空的可变数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM