Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么可以';我不能用这个代码从委托获取上下文吗?_Iphone_Objective C_Nsmanagedobject_Nsmanagedobjectcontext - Fatal编程技术网

Iphone 为什么可以';我不能用这个代码从委托获取上下文吗?

Iphone 为什么可以';我不能用这个代码从委托获取上下文吗?,iphone,objective-c,nsmanagedobject,nsmanagedobjectcontext,Iphone,Objective C,Nsmanagedobject,Nsmanagedobjectcontext,我从以下代码中得到了一个令人恼火的模糊错误: GFree2AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; context = [delegate managedObjectContext]; context在.h文件中定义为NSManagedObjectContext,在委托中也是如此。所有正确的文件似乎都包含在内(除了.m文件中的,但无论是否包含在头文件中,程序都会引发相同的问题。) 所有正确的框架和

我从以下代码中得到了一个令人恼火的模糊错误:

GFree2AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
context = [delegate managedObjectContext];
context
在.h文件中定义为
NSManagedObjectContext
,在委托中也是如此。所有正确的文件似乎都包含在内(除了.m文件中的
,但无论是否包含在头文件中,程序都会引发相同的问题。)

所有正确的框架和东西都包括在内——当我开始项目时,我选择了“使用coredata来管理数据”或其他任何东西。所以肯定不会有问题吧

有没有更好的方法来做我想做的事?基本上,我不想一直通过不同的类传递上下文,直到我最终想要使用它(存储数据是我的应用程序的一小部分)

在控制台中,我得到的错误是:

    2010-08-28 13:09:24.726 GFree2[3912:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
...

    terminate called after throwing an instance of 'NSException'
    Program received signal:  “SIGABRT”.
如果我注释掉这行:
context=[delegate managedObjectContext]然后一切看起来都很好。(目前我还没有实际使用任何coredata或任何东西,因此没有更多与之相关的代码

感谢所有能够提供帮助或提供一些见解的人——这太复杂了

编辑:我的应用程序代理文件方法:

#pragma mark -
#pragma mark Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (NSManagedObjectContext *)managedObjectContext {

  if (managedObjectContext != nil) {
    return managedObjectContext;
  }

  NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  if (coordinator != nil) {
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator:coordinator];
  }
  return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from the application's model.
 */
- (NSManagedObjectModel *)managedObjectModel {
  if (managedObjectModel != nil) {
    return managedObjectModel;
  }

  NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"GFree" ofType:@"momd"];
  NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
  managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
  return managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
  }

  NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"GFree.sqlite"]];

  NSError *error = nil;
  persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

      Typical reasons for an error here include:
        * The persistent store is not accessible;
        * The schema for the persistent store is incompatible with current managed object model.

      Check the error message to determine what the actual problem was.

      If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

      If you encounter schema incompatibility errors during development, you can reduce their frequency by:
        * Simply deleting the existing store:
          [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

        * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
          [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

        Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

    */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }    

  return persistentStoreCoordinator;
}

#pragma mark -
#pragma mark Application's Documents directory

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
  return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
再次编辑:

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"GFree" ofType:@"momd"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
是错误所在的行。我不确定这是什么类型的文件,但我确定它显然找不到它或其他东西…:/知道我该怎么做吗

@kiamlaluno-很抱歉回滚了您的编辑,并不是有意的,只是好奇地想看看您所做的更改,并认为这会显示给我…但它只是完全删除了它们。哈哈

编辑生成结果:

DataModelCompile build/Debug-iphonesimulator/GFree2.app/GFree2.mom GFree2.xcdatamodel
cd "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2"
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/momc -XD_MOMC_TARGET_VERSION=10.6 "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/GFree2.xcdatamodel" "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/build/Debug-iphonesimulator/GFree2.app/GFree2.mom"

我认为问题出在
GFree2AppDelegate.m
文件中的
persistentstorecordinator
方法中

能否使用此文件中的
managedObjectModel
managedObjectContext
persistentStoreCoordinator
方法的确切代码更新您的问题

当您选中“使用核心数据进行存储”时,这些方法由XCode生成


顺便说一句,您不应该导入.m文件。

您会收到错误,因为GFree.momd不存在。来自NSURL的pathForResource:ofType:method的文档:

返回值资源文件的完整路径名,如果找不到文件,则返回nil

GFree.momd是在生成时从.xcdatamodeld文件生成的(在生成结果中查找以DataModelVersionCompile开头的行并将其展开)。是否删除或重命名了该文件

它仍然列在构建目标的编译源代码部分吗?(在Xcode扩展目标/[AppName]/compilesources中)


刚刚注意到您的调试日志显示该应用程序名为GFree2,但您的代码正在查找GFree.momd。您是否重命名了该项目?如果数据模型文件现在名为GFree2.xcdatamodeld,则您需要将导入momd文件的行更改为使用新的GFree2名称。

更新了我的帖子。我不导入任何.m文件…只导入.h文件-但我不确定我应该在.h文件还是在.m文件中导入文件。:/frameworks似乎自动放在.h文件中,那么这是否意味着它们不需要包含在.m文件中?您应该在.m文件中导入尽可能多的文件。在大多数情况下,您可以通过添加
@class MyClas来避免.h文件中出现警告s、 
接口前面的行
…通常,您必须仅为继承自的类或声明您的类实现了位于某个文件中的协议的类添加import-in.h文件…关于您的问题,请尝试在核心数据方法中的所有
fileURLWithPath
中填充要记录的打印(一前一后)然后找到问题的根源…谢谢@michael-这里有很多有用的建议。我使用了断点和我在上面的原始帖子中提到的应用程序断点。我找不到该文件-我不确定应该在哪里查找它/它是否真的存在。嗯…我去掉了persistentStorCoodinator方法。:/找到了一种用更少代码实现的方法。不知道苹果为什么认为你需要这么复杂的方法字符串。您好。回到工作中-很抱歉回复太晚。我有一个GFree.xcdatamodel文件..?但我必须手动完成,我自己命名。我在构建结果中查看了这一行,但它没有告诉我任何事情。我我把电话线放在我的OP里了。