gpt4 book ai didi

iphone - 将新对象插入核心数据

转载 作者:行者123 更新时间:2023-11-28 20:36:26 25 4
gpt4 key购买 nike

我正在尝试将一个 NSData 对象插入到 CoreData 中,因此我不得不在事后将所有内容添加到我的应用程序中,因为我从未在选择 coredata 的情况下启动它。没关系,因为我已经开始了一个新项目并复制了所有适当的代码以完成所有设置。

我现在正从那里尝试设置 insertNewObject 方法。然而,这给我带来了一些问题。

我首先尝试从这样的另一种方法向它传递一些 NSData

[self insertNewObject:myData];

然后我像这样使用 insertNewObject 方法

- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];

// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:sender forKey:@"manufacturers"]; //not sure if this is correct, but sender has myData, and @"manufactures" is the attribute of my entity.

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}

从这里发生的事情是,当线程到达第三行“NSManagedObject *newManagedObject ...”时,应用程序崩溃并且我收到此错误

> 2012-04-27 11:18:21.579 thecode[1452:fb03] *** Terminating app due to
> uncaught exception 'NSInternalInconsistencyException', reason:
> '+entityForName: could not locate an NSManagedObjectModel for entity
> name 'Entity''
> *** First throw call stack: (0x14b5022 0x1952cd6 0x61d47 0xa8102 0x1a595 0x199a4 0x18b99 0x15097 0x1136a49 0x1134e84 0x1135ea7
> 0x1134e3f 0x1134fc5 0x1079f5a 0x1c02a39 0x1ccf596 0x1bf9120 0x1ccf117
> 0x1bf8fbf 0x148994f 0x13ecb43 0x13ec424 0x13ebd84 0x13ebc9b 0x215b7d8
> 0x215b88a 0x733626 0x762d 0x1c75 0x1) terminate called throwing an
> exception

我已经在我的 xcdatamodeld 中设置了为此所需的实体和属性

如有任何帮助,我们将不胜感激。

最佳答案

我不知道您的 CoreData 模型,但我猜您没有名为“Entity”的实体,因此当您尝试在此处插入名为“Entity”的新实体时,这可能会导致崩溃:

[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];

当您在上面一行获取实体描述但以后再也没有使用它时,我猜您正试图插入该实体。所以你要做的基本上是:

    [NSEntityDescription insertNewObjectForEntityForName:entity.name inManagedObjectContext:context];

希望这对您有所帮助!

关于iphone - 将新对象插入核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10342864/

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