gpt4 book ai didi

objective-c - NSManagedObjectContext在Core Data应用程序(iOS)中返回nil

转载 作者:行者123 更新时间:2023-12-01 18:25:35 25 4
gpt4 key购买 nike

我试图在每次按下按钮时将字符串保存到数据库中,但是当我运行该项目时,会在控制台上找到它:'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Info''

关于数据模型,我创建了一个.xcdatamodel,其模型名为“Info”,其内部为名为“path”的属性,其类型为字符串。

我创建了三个函数。 “enterdata”通过调用“findData”检查名称是否可用。如果名称可用,则通过“newData”记录新数据,如果不是,则查找其他名称。

我一直在寻找一些类似的问题,并且发现了this。它说de ManagedObjectContext必须传递给View Controller,但我不明白这是什么意思。

这是我的.h代码:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

这是我的.m代码:
#import <CoreData/CoreData.h>

@synthesize managedObjectContext;

int iSavedNum = 1;
bool bCanSave;

//Enter data
- (IBAction) enterdata:(id)sender {

//Search if data is already registered
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/info%i.png",docDir, iSavedNum];
[self findData:path :@"path"];

//If data is already saved, save it with new name.
if (bCanSave == NO) {
for (iSavedNum = 1; bCanSave == YES; iSavedNum++) {
[self findData:path :@"path"];
if (bCanSave == YES) {
[self newData:path :@"path"];
}
}
} else {
[self newData:path :@"path"];
}

}

//Input new data
- (void) newData:(NSString *)value:(NSString *)key {

//Create ManagedObjectContext and ManagedObjectModel
__0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObjectModel *newRecord;

//Put the data to the Entity
NSString *entityName = @"Info";
newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[newRecord setValue:value forKey:key];

//Errors management and cheking
NSError *error;
[context save:&error];
NSLog(@"Info Saved. Value: %@ Key: %@", value, key);

}

//Find Data
- (void) findData:(NSString *)valor:(NSString *)key {

//Create ManagedObjectContext
__0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

//Call the Entity and make a request
NSString *entityName = @"Info";
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];

//Create predicate to call specific info
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%@ = %@)", key, valor];
[request setPredicate:pred];

//Errors management and creation of an array with found info
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];

//Set if the name is avaliable or not
if ([objects count] == 0) {
bCanSave = YES;
} else {
bCanSave = NO;
}
}

最佳答案

它确切地告诉您错误是什么:

nil不是合法的NSManagedObjectContext参数

这意味着在这一行上:

newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];

变量 contextnil。这意味着您的 managedObjectContext方法无法正常工作。您没有显示此内容,因此我们可以添加的内容不多。

关于objective-c - NSManagedObjectContext在Core Data应用程序(iOS)中返回nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082185/

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