1

我想每一个按钮被按下时,以字符串保存到数据库中,但是当我运行该项目,我得到我的控制台上返回零。的NSManagedObjectContext核心数据应用(iOS)

参照数据模型,我已经创建与实体名为“信息”和,在其内部,命名为“路径”与一种类型的字符串的属性的.xcdatamodeld。

我已经创建了三个功能。 “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; 
    } 
} 
+0

其中d你是否创建了托管对象上下文? '[self managedObjectContext]'做什么?错误消息表明它返回'nil'。 –

+0

在我的.h中有@属性(只读,强壮,非原子)NSManagedObjectContext * managedObjectContext;'它是在.m中合成的,所以它在理论上被声明。不是吗? – iosdevrocks

+2

据* *声明的实例变量和属性,但该值是'nil'。您必须从核心数据模型和持久性存储创建托管对象上下文。也许你在'AppDelegate'或你的代码中的其他地方做过这些?然后你必须从那里使用托管对象上下文。 –

回答

4

它告诉你的错误是什么:

无不是法律的NSManagedObjectContext参数

这意味着,在这条线:

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

可变contextnil。这意味着您的managedObjectContext方法无法正常工作。你不显示这个,所以我们可以添加更多的东西。

+0

从上面的注释看来,OP已经在应用程序委托中创建了MOC,但没有从该“全局”MOC中设置managedObjectContext属性。 –

0

application:didFinishLaunchingWithOptions:appDelegate

/*initiate the managed Object Context */ 
CoreDataManager *coreDataManager = [CoreDataManager sharedDataManager]; 
    coreDataManager.managedObjectContext = self.managedObjectContext; 

其中CoreDataManager是我的核心日期管理器,它明确地包含所有的核心数据保存,删除方法

或者

yourClassObject.managedObjectContext = self.managedObjectContext; 

所以上下文得到初始化

相关问题