我有一个3级嵌套的循环: 这可行。 它足够快,不会降低应用程序的运行速度。 我不喜欢 我非常反对这种嵌套方式。 我很确定我可以使用NSPredicate搜索来做同样的事情。 我的SQL不好,所以我不确定从哪里开始。 Group , Region和Boundary都是C ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有一个问题,我有2个数组(日期和描述),一个保留一个从datePicker中选择的日期,另一个是带有字符串的数组,两个数组都从CoreData获取。
-(void)generateLocalNotification {
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"AddEntrySettings"];
fetchRequest.resultType = NSDictionaryResultType;
NSArray *result = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSMutableArray *date = [result valueForKey:@"date"];
NSMutableArray *descriere = [result valueForKey:@"descriere"];`
if (date != nil) {
for (NSString *stringDate in date) {
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy h:mm a"];
[format setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
self.date = [format dateFromString:stringDate];
NSLog(@"LOG:%@",date);
localNotification.fireDate = [self.date dateByAddingTimeInterval:0];
localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
for (int i = 0; i < descriere.count; i++) {
localNotification.alertBody = descriere[i];
}
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = @{@"id" : @42};
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
}
}
}
当我尝试fireDate时,一切工作正常,每次当数组中的日期与本地时间匹配时,我都会收到一条通知,直到尝试添加alertBody时,每次为alertBody进行for循环时,它只会显示我的最后一个条目NSArray。 在CoreData中,我同时添加了两个条目。 我的错误在哪里? 我怎样才能每次都收到与我在CoreData中插入的日期相匹配的alertBody通知?
问题是这个for循环:
for (int i = 0; i < descriere.count; i++) {
localNotification.alertBody = descriere[i];
}
对于每个stringDate
,将迭代到您的描述数组中的最后一项。 你需要的是找到指数stringDate
的date
,然后找到在同一个索引处的字符串descriere
。
但是,有一种更简单的方法。 不要将result
解包到两个单独的数组中,只需从for循环中访问不同的值即可:
if (result != nil) {
for (NSDictionary *dict in result) {
NSString *stringDate = [dict objectForKey:@"date"];
// if necessary, test whether stringDate is nil here
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy h:mm a"];
[format setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
self.date = [format dateFromString:stringDate];
NSLog(@"LOG:%@",date);
localNotification.fireDate = [self.date dateByAddingTimeInterval:0];
localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
localNotification.alertBody = [dict objectForKey:@"descriere"];
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = @{@"id" : @42};
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.