我有一系列的字典,它们代表着闪存卡。键值对之一是pgFinishedNumber和闪存卡出现在工作簿中的页面。用户还在应用程序pagesDone中设置了一个值,以便通过工作簿跟踪他们的进度。
我使用一个块来遍历主闪存卡数组,并将卡片添加到另一个名为myDailyArray的数组中。我想移动页号大于pagesDone值的闪存卡,但我有两个问题。
当多个闪存卡具有相同的页号时,就会出现第一个问题。当发生这种情况时,myDailyArray中只包含第一张卡。第二个问题发生在用户指示他在没有新闪存卡的页码上时。当发生这种情况时,数组中的下一个闪存卡将提前添加。
在viewWillAppear:中,my块的代码如下所示:
- (void)viewWillAppear:(BOOL)animated {
//......some code
[theLevelArray.level1Array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSMutableDictionary *dictDaily = [theLevelArray.level1Array objectAtIndex:idx];
[self.myDailyArray addObject:dictDaily];
int pgFinishedNumber = [[dictDaily objectForKey: @"pgFinishedNumber"] intValue];
if (pagesDone <= pgFinishedNumber)
{
*stop = YES;
}
}]; 例如,工作簿第3页介绍了两个闪存卡。这两张卡的NSDictionarys都有3作为pgFinishedNumber的值。当我在用户默认的3中输入pagesDone,然后运行应用程序时,只有第一个被添加到myDailyArray中。当我将pagesDone更改为4时,不仅要将两个pgFinishedNumber值为"3“的闪存卡都添加到myDailyArray中,而且还要添加下一个对象,该对象的pgFinishedNumber值为5!我很困惑。
发布于 2012-05-07 20:40:31
当到达大于或等于pagesDone的第一个页码时,您将停止迭代。这意味着以下任何可能具有相同页码的页面都将被忽略。
当pgFinishedNumber大于(但不等于) pagesDone时,您应该停止迭代,并将if语句移到块的顶部(在添加页面之前)。
我认为这也会解决第二个问题,我认为这是因为if语句位于块的末尾。
像这样的事情应该有效:
NSArray *levelArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"pgFinishedNumber", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2], @"pgFinishedNumber", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"pgFinishedNumber", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"pgFinishedNumber", nil], nil];
NSMutableArray *myDailyArray = [NSMutableArray array];
int pagesDone = 4;
[levelArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSMutableDictionary *dictDaily = (NSMutableDictionary *)obj;
int pgFinishedNumber = [[dictDaily objectForKey: @"pgFinishedNumber"] intValue];
if (pagesDone < pgFinishedNumber) {
*stop = YES;
} else {
[myDailyArray addObject:dictDaily];
}
}];
NSLog(@"%@", myDailyArray);https://stackoverflow.com/questions/10488055
复制相似问题