Search code examples
objective-ccore-datansstringnsurl

Unable to convert a string to a NSURL


I am trying to turn off WAL in CoreData using this code:

//  Code to disable journaling mode
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlString = [applicationDocumentsDirectory stringByAppendingPathComponent: @"saori.sqlite"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSDictionary *options = @{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}};
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] init];
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil  URL:url options:options error:nil];

This is my NSString that I need to convert to a URL:

/Users/rolfmarsh/Library/Application Support/iPhone Simulator/7.1/Applications/7A614C51-AC51-4F5B-9716-6E3D2F160324/Documents/saori.sqlite

When I use that string in this statement, it generates nil.

    NSURL *url = [[NSURL alloc] initWithString:urlString];

I don't see anything wrong with the string. What am I doing wrong?


Solution

  • Read the docs for NSURL. You have a file path and you need to create a file URL from the file path.

    NSURL *url = [NSURL fileURLWithPath:urlString];