iPhone(iOS):将文件从主包复制到文档文件夹错误 [英] iPhone (iOS): copying files from main bundle to documents folder error
问题描述
我正在尝试将首次启动时从我的应用包中的一些文件复制到文档目录。我有第一次启动的检查,但为了清楚起见,它们未包含在代码段中。问题是我正在复制到文档目录(已经存在),在文档中,它说明:
I am trying to copy some files across from my app bundle to the documents directory on first launch. I have the checks in place for first launch, but they're not included in code snippet for clarity. The problem is that I am copying to the documents directory (which already exists) and in the documentation, it states that:
在操作之前,dstPath 不得存在。
对我来说,最好的方法是直接复制到文件根?我想这样做的原因是允许iTunes文件共享支持。
What is the best way for me to achieve the copying straight to the documents root? The reason I want to do this is to allow iTunes file sharing support.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];
NSLog(@"\nSource Path: %@\nDocuments Path: %@", sourcePath, documentsDirectory);
NSError *error = nil;
if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error]){
NSLog(@"Default file successfully copied over.");
} else {
NSLog(@"Error description-%@ \n", [error localizedDescription]);
NSLog(@"Error reason-%@", [error localizedFailureReason]);
}
...
return YES;
}
谢谢
推荐答案
您的目标路径必须包含要复制的项目的名称,而不仅仅是文档文件夹。尝试:
Your destination path must contain the name of item being copied, not just the documents folder. Try:
if([[NSFileManager defaultManager] copyItemAtPath:sourcePath
toPath:[documentsDirectory stringByAppendingPathComponent:@"Populator"]
error:&error]){
...
编辑:抱歉误解了您的问题。不知道是否有更好的选择,然后迭代文件夹内容并分别复制每个项目。如果你的目标是iOS4,你可以使用NSArray的 -enumerateObjectsUsingBlock:
函数:
Sorry misunderstood your question. Don't know if there's a better option then iterating through folder contents and copy each item separately. If you're targeting iOS4 you can use NSArray's -enumerateObjectsUsingBlock:
function for that:
NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];
[resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSError* error;
if (![[NSFileManager defaultManager]
copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj]
toPath:[documentsDirectory stringByAppendingPathComponent:obj]
error:&error])
DLogFunction(@"%@", [error localizedDescription]);
}];
P.S。如果你不能使用块你可以使用快速枚举:
P.S. If you can't use blocks you can use fast enumeration:
NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];
for (NSString* obj in resContents){
NSError* error;
if (![[NSFileManager defaultManager]
copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj]
toPath:[documentsDirectory stringByAppendingPathComponent:obj]
error:&error])
DLogFunction(@"%@", [error localizedDescription]);
}
这篇关于iPhone(iOS):将文件从主包复制到文档文件夹错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!