gpt4 book ai didi

cocoa - 如何在 Mac 应用程序中设置初始核心数据存储?

转载 作者:行者123 更新时间:2023-12-03 16:47:59 26 4
gpt4 key购买 nike

我是一名经验丰富的 iOS 开发人员,正在尝试制作我的第一个 Mac 应用程序。我想使用核心数据将数据存储在我的应用程序中。在我的 iOS 应用程序中,我通常有一个预先创建的 SQLite 文件,用作数据存储的初始状态,并在应用程序第一次运行时移动到位,如下所示:

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

我想在 mac 应用程序中执行类似的操作,只不过将数据放在 ~/Library/Application Support/MyApp 目录中。我似乎不知道该怎么做。有什么指点吗?

最佳答案

在 Xcode 4 中,由于某种原因,默认的 Core Data 项目模板使用根 ~/Library 目录(即 ~/Library/Application 来存储你的 Core数据文件。无论如何你都应该更改它(因为这是一个坏主意),但是一旦你这样做了,它应该按照你的预期工作。我相信 Mac 上的默认名称是 storedata,你应该另请注意,您需要将存储类型从 XML 更改为默认值。

关于cocoa - 如何在 Mac 应用程序中设置初始核心数据存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6475593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com