在应用程序中传送预填充的 SQLite 数据库。启动时数据库的路径
也许问题的标题不是很明显,但我不知道如何总结。
我正在完成一个应用程序,它基本上是一个数据库。我将在应用程序内发送一个数据库。一切正常,但现在我遇到了问题。安装App时,需要在设备上创建数据库。因此,我将 DB 拖到 Xcode 项目中的“Supporting Files”文件夹(顺便说一句,仍然是 Xcode 4.1)。我转到 AppDelegate.m 文件并查找 (NSPersistentStoreCoordinator *)persistentStoreCoordinator{} 方法。
以下行替换了
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];
我用
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];
NSLog(@"%@", storePath);
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
此代码:我从 Ray Wenderlich 的教程。
问题是编译器在
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];
行上向我发出警告这告诉我“NSURL 可能不会响应‘stringByAppendingPathComponent’,实际上,应用程序因此而崩溃。有趣的是,教程中的示例没有给出任何警告。
有人可以帮助我解决我缺少的内容吗?
谢谢进步!
Maybe the title of the question is not very self-explanatory but I don't know how to summ it up.
I'm finishing an App which is basically a DB. I will ship a DB within the App. Everything works well but now I came to the problem. When the App is installed, it needs to create the database on the device. So, I dragged the DB to the "Supporting Files" folder in my Xcode project (still Xcode 4.1, by the way). I went to the AppDelegate.m file and looked for the (NSPersistentStoreCoordinator *)persistentStoreCoordinator{} method.
I substituted the line:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];
for this code:
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];
NSLog(@"%@", storePath);
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
I got that from a tutorial by Ray Wenderlich.
The thing is that the compiler gives me a warning on the line
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"];
that tells me "NSURL may not respond to 'stringByAppendingPathComponent' and, actually, the App crashes because of that. interestingly enough, the example from the tutorial gives no warning whatsoever.
Somebody could give me a hand in what am I missing?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定 [self applicationDocumentsDirectory] 返回 NSString 吗?如果没有,请尝试此
并将 [self applicationDocumentsDirectory] 更改为 [self applicationDocumentsDirectoryString]
Are you sure [self applicationDocumentsDirectory] returns a NSString? If not, try this
And change [self applicationDocumentsDirectory] to [self applicationDocumentsDirectoryString]