bundle sqlite database with app and stringByAppendingPathComponent error

落爺英雄遲暮 提交于 2019-12-11 01:18:34

问题


There are probably many other questions I don't even know to ask yet since I'm new to app programming.

I initially created the database from within the app, copied it to my working folder (which is probably not where it should ultimately reside), then appended my records (about 1,000 of them) from a text file.

The first two questions that come to mind are: - what folder should the database be in? - how does it get deployed with the app?

I found quite few examples using the following lines in persistentStoreCoordinator function:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myDatabase.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

But the first line gives me the pre-compile error: "Receiver type 'NSURL' for instance message does not declare a method with selector 'stringByAppendingPathComponent:'. Why is it not working for me?

And is this in fact the best way to bundle my database with the rest of the app?

Thanks!


回答1:


Easiest solution is to use NSUrl instead of NSString. SO user @trapper already provided a solution in the below link.

importing sqlite to coredata

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];

// If the database doesn't exist copy in the default one
if (![storeURL checkResourceIsReachableAndReturnError:NULL])
{
    NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Database" withExtension:@"sqlite"];

    if ([defaultStoreURL checkResourceIsReachableAndReturnError:NULL])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
    }
}


来源:https://stackoverflow.com/questions/8731620/bundle-sqlite-database-with-app-and-stringbyappendingpathcomponent-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!