Coredata nsurl issue may not respond to stringByAppendingPathComponent

I had problems after starting a new coredata project with xcode 3.2.5 ... my previous projects with the main data (in the previous xcode) worked fine, so I don’t know what the difference is?

so the error that I get when I create and go to the view that calls the main data is β†’

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 

it is strange that in my * AppDelegate.m, in (edited thanks to Rog, but still does not work!)

 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator_ != nil) { return persistentStoreCoordinator_; } NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"]; NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; //new position for the storeUrl! // Put down default db if it doesn't already exist NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:storePath]) { NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"staff" ofType:@"sqlite"]; if (defaultStorePath) { [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; } } 

in

  NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"]; 

I get a warning

 NSURL may not respond to '-stringByAppendingPathComponent' 

I option + click this line ByAppendingPathComponent and get (character not found !!!!)

but in other projects I make the option + click in the same and get the definition !!

  • so is this a warning related to my error ??
  • how to fix it ???

Edit, included this in my viewDidLoad

NSLog (@ "path =% @", [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]);

which gives me the console:

 path= /Users/mkss9/Library/Application Support/iPhone Simulator/4.2/Applications/2F364C20-2B87-4ABB-AA3E-FB6F7C15096F/Documents 

please !, im going crazy !!

Thank you!

+4
source share
3 answers

Some versions of the SDK back (I don’t know exactly when they did this), Apple changed the return type of applicationDocumentsDirectory in its project templates.

When you create a new project, it looks like this:

 /** Returns the URL to the application Documents directory. */ - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } 

in old templates, it looked like this:

 /** Returns the path to the application documents directory. */ - (NSString *)applicationDocumentsDirectory { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return basePath; } 

and between them it looked like this:

 /** Returns the path to the application Documents directory. */ - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } 

So you have to be careful, because all old code that relies on applicationDocumentsDirectory returning NSString will not work with newer templates.

And you cannot just replace the new version with an older version, because it will change your basic data methods.

Therefore, I suggest you write your own method for returning a catalog of documents. Apple often changes its applicationDocumentsDirectory .

+7
source

I would think, because -applicationDocumentsDirectory returns NSURL * instead of NSString * .

+1
source

First, you must ensure that the applicationDocumentsDirectory method returns an NSString.

After this has happened, the subsequent failure is due to the fact that you are passing the path and file name that do not yet exist.

So, if you move your NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; after the code that checks the existing file and sets it by default, if it does not exist, it should solve your problem.

0
source

Source: https://habr.com/ru/post/1339299/


All Articles