开发者

How to set up initial Core Data store in a Mac app?

I am an experienced iOS developer trying to make my first Mac app. I want to use Core data to store the data in my app. In my iOS apps, I generally have a pre-created SQLite file which is used as the initial state of the data store, and which is moved into place on the first time the app is run, like this:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Datafile.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle]  pathForResource:@"Datafile-DefaultData" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

I want to do something similar in the mac app, except put the data in the ~/Library/Application Suppo开发者_StackOverflow中文版rt/MyApp directory. I can't seem to figure out how to do it. Any pointers?


In Xcode 4, the default Core Data project template, for some reason, uses the root ~/Library directory (ie ~/Library/Application to store your Core Data file. You should change this anyways (because it's a bad idea), but once you do that, it should work as you expect. I believe the default name on the Mac is storedata, and you should also note that you'll need to change the store type from XML, which is the default.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜