4

目前,我正在遍历所有计划的本地通知,以根据 userInfo 字典对象中的值查找“匹配”。当我设置了 30 多个本地通知时,这似乎非常慢。有没有办法在不遍历数组的情况下访问单个本地通知?

这是我所拥有的:

NSArray *notificationArray = [[UIApplication sharedApplication]     scheduledLocalNotifications];
UILocalNotification *row = nil;
for (row in notificationArray) {
            NSDictionary *userInfo = row.userInfo;
            NSString *identifier = [userInfo valueForKey:@"movieTitle"];
            NSDate *currentAlarmDateTime = row.fireDate;
if([identifier isEqualToString:myLookUpName]) {
 NSLog(@"Found a match!");
}
}

这是我想要的:

NSArray *notificationArray = [[UIApplication sharedApplication]     scheduledLocalNotifications];
UILocalNotification *row = " The row in notificationArray where [userInfo valueForKey:@"movieTitle"]=myLookUpName" ;
4

1 回答 1

8

您可能可以为此使用谓词,但我尚未对其进行测试:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userInfo.movieTitle = %@", myLookUpName];

然后使用该谓词过滤数组并获取第一个元素:

UILocalNotification *row = [[notificationArray filteredArrayUsingPredicate:predicate]objectAtIndex:0];

同样,这是未经测试的,可能无法正常工作。

编辑

如果这不起作用,您可以使用测试块:

UILocalNotification *row = [[notificationArray objectsAtIndexes:[notificationArray indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    return [[[obj userInfo]valueForKey:@"movieTitle"] isEqualToString:myLookUpName];
}]]objectAtIndex:0];
于 2012-04-15T19:00:34.200 回答