开发者

'-[NSURL initFileURLWithPath:]: nil string parameter' when using CoreData

I have some model classes which I wanted to store with the CoreData framework. I have created the xcdatamodel file and added these already existing classes to the CoreData model file. I have also copied the code responsible for managing the context, etc. from the core data project template from the XCode.

Now I have the following code (it was copied, only the file name was changed to HistoryDataModel):

- (NSManagedObjectModel *)managedObjectModel {

   if (managedObjectModel != nil) {
       return managedObjectModel;
   }
   NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"HistoryDataModel"  ofType:@"momd"];
   NSLog(@"Model path:", modelPath);
   NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
   managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
   return managedObjectModel;
}

Now when I run this code and try to access data in the CoreData I'm getting the following error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFi开发者_如何学编程leURLWithPath:]: nil string parameter'

and it happens when

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"HistoryDataModel" ofType:@"momd"];

is being executed (the model Path is an empty string).

Any ideas what I might have done wrong? Thanks!


You could try the following snippet, which will save you a lot of time :)

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];

It returns a model created by merging all the models found in all the bundles (nil parameter). You can set the parameter to a specific array of bundles.


Is your core data model versioned already? If not then the correct extension should be mom

To create a versioned model you select the model and run Design/Data Model/Add Model Version in the menu.


I just had the same issue and here is how I resolved it: Here is my old code

    [self.PDFWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:pdfFileName ofType:@"pdf"]]]];

Here is my resolved code:

    [self.PDFWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:  [[NSBundle mainBundle]pathForResource:pdfFileName ofType:@"PDF"]]]];

As you can see, the first line has ofType:@"pdf" while the second has ofType:@"pdf". All my pdf files included in the app ended in .PDF, so I was getting a nil error when I was looking for file type pdf.

Obvious error, but I thought IOS would be smart enough to check for both .pdf and .PDF, but I was wrong. Perhaps that is the issue this person was having.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜