日历事件应用程序的核心数据谓词
Core data predicate for a calendar event app
我是一名新手程序员,我已经在这个问题上苦苦挣扎了两个星期。我正在使用来自 cocoapods 的 JTCalendar。它旨在 return - (BOOL)calendarHaveEvent:(JTCalendar *)calendar date:(NSDate *)date
{
下的日期
它将在日历上显示为一个点以显示事件。为了保存我的活动,我使用以下代码:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *savedate;
savedate = [NSEntityDescription insertNewObjectForEntityForName:@"AlarmData" inManagedObjectContext:context];
[savedate setValue:CT forKey:@"alarmDate"];
[context save:&error];
BOOL isSaved = [appDelegate.managedObjectContext save:&error];
NSLog(@"success!!!!, %@", savedate);
和以下尝试获取日期和 "return" 它:
'
- (BOOL)calendarHaveEvent:(JTCalendar *)calendar date:(NSDate *)date
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *textDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(@"Date %@",textDate);
NSDate *startDate = [dateFormatter dateFromString:@"31-12-1981"];
NSDate *endDate = [dateFormatter dateFromString:@"31-12-2999"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(alarmDate >= %@) AND (date <= %@)", startDate, endDate];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AlarmData" inManagedObjectContext:context]];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
NSString *alarmDate = [ NSString stringWithFormat:@"dd-MM-yyyy"];
for (NSManagedObject *obj in matchingData) {
alarmDate = [obj valueForKey:@"alarmDate"];
return alarmDate;'
遗憾的是,结果看起来像这样,每个日期似乎都 return 下方有一个点:
https://drive.google.com/file/d/0Bw0Kf2Z9-yb8Zm5LWnZUUkRJRzQ/view?usp=sharing
我只想有我的单身约会returned,卡在这上面好久了。谢谢!
for (NSManagedObject *obj in matchingData) {
alarmDate = [obj valueForKey:@"alarmDate"];
return alarmDate;'
您总是 return 第一个对象的 alamrDate,无论您的方法参数中的日期是什么。使用您的参数检查日期,然后 return 是否有针对该日期的警报。
我是一名新手程序员,我已经在这个问题上苦苦挣扎了两个星期。我正在使用来自 cocoapods 的 JTCalendar。它旨在 return - (BOOL)calendarHaveEvent:(JTCalendar *)calendar date:(NSDate *)date
{
下的日期
它将在日历上显示为一个点以显示事件。为了保存我的活动,我使用以下代码:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *savedate;
savedate = [NSEntityDescription insertNewObjectForEntityForName:@"AlarmData" inManagedObjectContext:context];
[savedate setValue:CT forKey:@"alarmDate"];
[context save:&error];
BOOL isSaved = [appDelegate.managedObjectContext save:&error];
NSLog(@"success!!!!, %@", savedate);
和以下尝试获取日期和 "return" 它:
'
- (BOOL)calendarHaveEvent:(JTCalendar *)calendar date:(NSDate *)date
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *textDate = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(@"Date %@",textDate);
NSDate *startDate = [dateFormatter dateFromString:@"31-12-1981"];
NSDate *endDate = [dateFormatter dateFromString:@"31-12-2999"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(alarmDate >= %@) AND (date <= %@)", startDate, endDate];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AlarmData" inManagedObjectContext:context]];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
NSString *alarmDate = [ NSString stringWithFormat:@"dd-MM-yyyy"];
for (NSManagedObject *obj in matchingData) {
alarmDate = [obj valueForKey:@"alarmDate"];
return alarmDate;'
遗憾的是,结果看起来像这样,每个日期似乎都 return 下方有一个点:
https://drive.google.com/file/d/0Bw0Kf2Z9-yb8Zm5LWnZUUkRJRzQ/view?usp=sharing
我只想有我的单身约会returned,卡在这上面好久了。谢谢!
for (NSManagedObject *obj in matchingData) {
alarmDate = [obj valueForKey:@"alarmDate"];
return alarmDate;'
您总是 return 第一个对象的 alamrDate,无论您的方法参数中的日期是什么。使用您的参数检查日期,然后 return 是否有针对该日期的警报。