来自核心数据的 For 循环
For Loop from Core Data
我有一个问题,我有 2 个数组(日期和描述),一个保存我从 datePicker select 得到的日期,另一个是带有字符串的数组,两个数组都是从 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
迭代到描述数组中的最后一项。你想要的是在date
中找到stringDate
的索引,然后在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];
}
}
我有一个问题,我有 2 个数组(日期和描述),一个保存我从 datePicker select 得到的日期,另一个是带有字符串的数组,两个数组都是从 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
迭代到描述数组中的最后一项。你想要的是在date
中找到stringDate
的索引,然后在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];
}
}