我刚开始使用iPhone开发.在我必须在tabbar控制器的表视图中显示存储在sqlite db中的一些数据的示例之一中,我不得不将sqlite文件从应用程序包移动到documents文件夹.
我使用了应用程序模板 - 用于iPhone的iOS>应用程序>基于窗口的应用程序(用于存储的已用核心数据)
在由XCode生成的模板中(基本sdk设置为最新iOS = 4.2),以下代码在那里......
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
在尝试获取文档文件夹的路径时,我使用上面给出的方法,就像这样......
NSString *documentDirectory = [self applicationDocumentsDirectory];
Run Code Online (Sandbox Code Playgroud)
这给了一个警告 - warning: incompatible Objective-C types initializing 'struct NSURL *', expected 'struct NSString *'
所以我把代码更改为以下...
// Added the message absoluteString over here
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];
NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
Run Code Online (Sandbox Code Playgroud)
和砰!它给出了错误 - 'Failed to create writable database file with message 'The operation couldn’t be completed. No such file or directory'.'
我应该如何找到文档目录的路径,因为applicationDocumentsDirectory
XCode模板生成的方法对我不起作用.
此外,有人可以对上述applicationDocumentsDirectory
方法的目的进行一些说明.
谢谢
Ric*_*ich 20
这是一个简单的方法:
NSFileManager *fmngr = [[NSFileManager alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mydb.sqlite" ofType:nil];
NSError *error;
if(![fmngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/mydb.sqlite", NSHomeDirectory()] error:&error]) {
// handle the error
NSLog(@"Error creating the database: %@", [error description]);
}
[fmngr release];
Run Code Online (Sandbox Code Playgroud)
我几天前就碰到了这个.不要强行沿路径路径,包含NSURL路径.获得如何使用它们不会花费很短的时间.
至于方法,它只是要求系统将URL提供给应用程序的标准化文档目录.使用此以及关于放置新文件的位置的大部分内容都是正确的.
归档时间: |
|
查看次数: |
14157 次 |
最近记录: |