gpt4 book ai didi

ios - 将所有文件从文档/收件箱移动到 iOS 应用程序中的文档

转载 作者:行者123 更新时间:2023-11-29 12:06:25 27 4
gpt4 key购买 nike

我有一个 iOS 应用程序,我已经在我的应用程序中为 .plist 文件实现了“打开方式”功能。一切正常,除了当我导入 .plist 文件时,它们最终出现在文档/收件箱中而不是常规文档文件夹中。有没有办法将其更改为显示在文档文件夹中而不是收件箱文件夹中,或者移动它们?我目前正在使用以下代码,但它似乎没有做任何事情。代码来自this上的第3个回复页面。

//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@";%@/Inbox", documentsDirectory] error:nil];

//move all the files over
for (int i = 0; i != [inboxContents count]; i++) {
NSString *oldPath = [NSString stringWithFormat:@";%@/Inbox/@%", documentsDirectory, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:@";%@", documentsDirectory, [inboxContents objectAtIndex:i]];
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
}

如果可能的话,我还想在所有文件传输完毕后清除收件箱文件夹,但这不是优先事项,因为 plist 文件往往非常小。

编辑:我发现(根据用户 originaluser2 关于使用断点的建议)我的应用程序没有正确选取目录,所以我更改了代码,以便它选取预设的字符串。这是 viewDidLoad 中的当前代码

//Turn every file inside the directory into an array
// Note to self: remember to actually put files in the Documents folder. Use the code in the apparopriately marked file
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//strings to actually get the directories
NSString *appFolderPath = [path objectAtIndex:0];
NSString *inboxAppFolderPath = [appFolderPath stringByAppendingString:@"/Inbox"]; //add ".plist" to the end of the recipe name


//predicates to hide unwanted files
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not SELF beginswith[c] '.DS_Store'"];
NSPredicate *inboxPredicate = [NSPredicate predicateWithFormat:@"not SELF beginswith[c] 'Inbox'"];
recipes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appFolderPath error:nil];
recipes = [recipes filteredArrayUsingPredicate:predicate];
recipes = [recipes filteredArrayUsingPredicate:inboxPredicate];




//move all files from inbox to documents root
//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:inboxAppFolderPath, documentsDirectory] error:nil];

//move all the files over
for (int i = 0; i != [inboxContents count]; i++) {
NSString *oldPath = [NSString stringWithFormat:inboxAppFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:appFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
}
//end of moving all files

使用此代码,应用程序可以正确查看目录并告诉我收件箱文件夹中有哪些文件,但实际上不会将其内容传输到文档文件夹。

最佳答案

问题出在您定义的路径上:

NSString *oldPath = [NSString stringWithFormat:inboxAppFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:appFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];

在为 moveItemAtPath: 方法提供一个 NSError 之后,它返回了错误:

2016-01-19 08:43:57.534 Moving Files[35505:9385200] error: “Inbox” couldn’t be moved to “E70C5B67-D502-4C06-B480-03675E35E999” because an item with the same name already exists.

发生的事情是您的字符串格式不正确,因为它只采用您提供的第一个参数,即收件箱文件夹路径(而不是收件箱文件夹中的文件路径)。

您只想将路径定义更改为以下内容:

NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inboxAppFolderPath, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:@"%@/%@", appFolderPath, [inboxContents objectAtIndex:i]];

这应该可以解决问题。

请记住,以后如果您在使用 NSFileManager 时遇到问题,请始终尝试从中找出错误:

NSError* error;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
NSLog(@"error: %@", error.localizedDescription);

关于ios - 将所有文件从文档/收件箱移动到 iOS 应用程序中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835213/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com