【问题标题】:Unable to convert a string to a NSURL无法将字符串转换为 NSURL
【发布时间】:2014-07-12 21:04:43
【问题描述】:

我正在尝试使用以下代码关闭 CoreData 中的 WAL:

//  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];

这是我需要转换为 URL 的 NSString:

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

当我在此语句中使用该字符串时,它会生成 nil

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

我看不出字符串有什么问题。我做错了什么?

【问题讨论】:

    标签: objective-c core-data nsstring nsurl


    【解决方案1】:

    阅读NSURL 的文档。你有一个文件路径,你需要从文件路径创建一个文件 URL。

    NSURL *url = [NSURL fileURLWithPath:urlString];
    

    【讨论】:

    • 我阅读了文档,但速度非常快,而且像往常一样,我错过了那个...再次感谢 Rick...感谢您的帮助(和往常一样)...:D
    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2016-06-04
    • 2016-02-04
    相关资源
    最近更新 更多