iPhone:如何将文件从资源复制到文档? [英] iPhone: How to copy a file from resources to documents?

查看:150
本文介绍了iPhone:如何将文件从资源复制到文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用iPhone开发。在我必须在tabbar控制器的表视图中显示存储在sqlite db中的一些数据的示例中,我不得不将sqlite文件从应用程序包移动到文档文件夹。

I just started with iPhone development. In one of the example where I had to display some data stored in sqlite db in a table view in a tabbar controller, I had to move a sqlite file from application bundle to the documents folder.

我使用了应用程序模板 - 用于iPhone的iOS>应用程序>基于窗口的应用程序(用于存储的已用核心数据)

I used the application template - iOS > Application > Window-based application for iPhone (Used core data for storage)

在XCode生成的模板中( base sdk设置为最新iOS = 4.2),以下代码在那里......

In the template generated by XCode (base sdk set to latest iOS = 4.2), the following code was there...

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

在尝试获取文档文件夹的路径时,我使用了上面给出的方法像这样......

In trying to get a path to documents folder, I used the method given above like this...

NSString *documentDirectory = [self applicationDocumentsDirectory];

这给出了一个警告 - 警告:不兼容的Objective-C类型初始化'struct NSURL *',预期'struct NSString *'

This gave a warning - warning: incompatible Objective-C types initializing 'struct NSURL *', expected 'struct NSString *'

所以我将代码更改为以下...

So I changed the code to following...

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

和BOOM !!!它给出了错误 - '无法创建带有消息的可写数据库文件'操作无法完成。没有这样的文件或目录'。'

And BOOM!!! It gave the error that - 'Failed to create writable database file with message 'The operation couldn’t be completed. No such file or directory'.'

我应该如何找到文件目录的路径,因为方法 applicationDocumentsDirectory <由XCode模板生成的/ code>对我不起作用。

How should I find the path to documents directory since the method applicationDocumentsDirectory generated by XCode template does not work for me.

此外,有人可以说明上面给出的 applicationDocumentsDirectory 方法的目的。

Also, can somebody throw some light on the purpose of applicationDocumentsDirectory method given above.

谢谢

推荐答案

我几天就遇到了这个问题前。不要强行沿路径路径,包含NSURL路径。获取如何使用它们不会花费很短的时间。

I just ran into this myself a few days ago. Don't force things down the Path path, embrace the NSURL path. It won't take but a short time to get how to use them.

关于方法,它只是要求系统将URL提供给标准化文档应用程序的目录。使用此以及关于放置新文件的位置的大部分内容都是正确的。

As to the method, it's simply asking the system to hand you a URL to the standardized documents directory for the application. Use this and most everything regarding where you put new files will be correct.

这篇关于iPhone:如何将文件从资源复制到文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆