目前,我正在循环遍历所有计划的本地通知,以根据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" ;
答案 0 :(得分: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];