Macos 在基于CoreData文档的应用程序中加载默认数据库
我有一个基于文档的应用程序,我希望在创建新文件时,tableview会预先填充默认数据。 我做了以下工作:Macos 在基于CoreData文档的应用程序中加载默认数据库,macos,core-data,nsfilemanager,nsdocument,Macos,Core Data,Nsfilemanager,Nsdocument,我有一个基于文档的应用程序,我希望在创建新文件时,tableview会预先填充默认数据。 我做了以下工作: 我启动了应用程序并填充了表,然后保存了文件Untitled.sqlite 我在应用程序的资源之间导入了文件Untitled.sqlite 我在下面的文件中插入了下面的代码 MyApplicationDocumente.m: - (id)init { self = [super init]; if (self) { // Add your subclass-specific init
Untitled.sqlite
Untitled.sqlite
- (id)init {
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:@"/Users/andreabollini/Library/Developer/Xcode/DerivedData/Preventivi-euqsrtroavapbwfjvljrxamywbvf/Build/Products/Debug/Preventivi.app/Contents/Resources/en.lproj"]) {
NSString *defaultStorePath = [[NSBundle mainBundle]
pathForResource:@"Untitled" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:@"/Users/andreabollini/Library/Developer/Xcode/DerivedData/Preventivi-euqsrtroavapbwfjvljrxamywbvf/Build/Products/Debug/Preventivi.app/Contents/Resources/en.lproj" error:NULL];
[tabella1 reloadData];
[tabella2 reloadData];
}
}
}
return self;}
没有错误,但它不起作用。当我打开应用程序时,tableview仍然是空的,什么也没有发生。怎么了?我按照你说的做了,但什么都没有改变。这是iOS还是OS X应用程序?表视图是如何填充的?它是一个用于OS X的应用程序。有两个实体具有两个表视图,具有对多关系。@Andrea:复制文件后,您是否应该以某种方式打开数据库?@Andrea:另一点:您将“Untitled.sqlite”复制到编译应用程序的某个文件夹中。如果将应用程序移动到其他文件夹,则此操作将不起作用。