gpt4 book ai didi

ios - 核心数据对象通过循环更新

转载 作者:行者123 更新时间:2023-11-28 19:05:19 24 4
gpt4 key购买 nike

我有一个显示核心数据对象tableView。在同一个 View 中有五个按钮。每个按钮操作都应该更新对象的属性值。作为示例,我将向您展示我必须更新属性“isDone”:

- (IBAction)allDoneAction:(id)sender {

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
int i=0;
for (NSManagedObject *mo in context)
{
[mo setValue:@"Done" forKey:@"isDone"];i++;
}

[managedObjectContext save:nil];

}

此方法抛出以下异常:

NSManagedObjectContext countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9a6b0a0
2014-01-06 19:01:43.862 To-Do Pro[679:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9a6b0a0'

我需要什么来避免异常并获得所需的更新?这是我的 NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController) return fetchedResultsController;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity =
[NSEntityDescription entityForName:@"FavoriteThing"
inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"displayOrder"
ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortDescriptor, nil];
//SOLO TO-DOS DE TODAY


todayDate = [NSDate date];

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components




NSNumber *yearBuscado = [NSNumber numberWithLong:[components year]];
NSNumber *mesBuscado = [NSNumber numberWithLong:[components month]];
NSNumber *diaBuscado = [NSNumber numberWithLong:[components day]];

// NSString *tipourgente = @"Urgent";
// NSString *tipocolor = @"Yellow";

NSString *textoNotDone = @"Not done";
NSString *textoNotDeleted = @"Not deleted";

NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"todoYear == %@", yearBuscado];
NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"todoMonth == %@", mesBuscado];
NSPredicate *dayPredicate = [NSPredicate predicateWithFormat:@"todoDay == %@", diaBuscado];
NSPredicate *notDonePredicate = [NSPredicate predicateWithFormat:@"isDone== %@", textoNotDone];
NSPredicate *notDeletedPredicate = [NSPredicate predicateWithFormat:@"isSemiDeleted==%@", textoNotDeleted];
// NSPredicate *urgentPredicate = [NSPredicate predicateWithFormat:@"urgent == %@", tipourgente];
// NSPredicate *colorPredicate = [NSPredicate predicateWithFormat:@"color == %@", tipocolor];

[fetchRequest setSortDescriptors:sortDescriptors];






NSPredicate *busqueda = [NSCompoundPredicate andPredicateWithSubpredicates:@[yearPredicate,monthPredicate,dayPredicate,notDonePredicate,notDeletedPredicate]];

[fetchRequest setPredicate:busqueda];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
[self setFetchedResultsController:aFetchedResultsController];


[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];


return fetchedResultsController;
}

最佳答案

好吧,我对核心数据一无所知,但从我最近两分钟的研究来看,你似乎没有循环访问 NSManagedObjectContext 的对象。您需要在上下文中创建搜索并从中获取结果。然后遍历结果并随意修改。

这是取自 this answer 的示例:

NSManagedObjectContext * context = [self managedObjectContext];
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"ShoppingBasket" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id basket in result)
[context deleteObject:basket];

因此,获取上下文、创建搜索、根据搜索条件从上下文中获取数组、遍历结果并按照您的意愿更新它们。

关于ios - 核心数据对象通过循环更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20962749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com