Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将所有文件从文档/收件箱移动到iOS应用程序中的文档_Ios_Objective C_Directory - Fatal编程技术网

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

将所有文件从文档/收件箱移动到iOS应用程序中的文档,ios,objective-c,directory,Ios,Objective C,Directory,我有一个iOS应用程序,我已经在我的应用程序中为.plist文件实现了“打开”功能。除了导入.plist文件时,它们最终会出现在documents/inbox中,而不是普通的documents文件夹中之外,其他一切都可以正常工作。是否有办法将其更改为显示在“文档”文件夹而不是“收件箱”文件夹中,或者移动它们?我目前正在使用下面的代码,但它似乎没有做任何事情。代码来自第页的第3个回复 如果可能的话,我也希望在传输完所有文件后清除收件箱文件夹,但这不是优先事项,因为plist文件往往非常小 编辑:我

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

如果可能的话,我也希望在传输完所有文件后清除收件箱文件夹,但这不是优先事项,因为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

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

问题在于您定义的路径:

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);

为什么在
stringWithFormat:@”%@/Inbox“
”中有分号?@originaluser2我不太确定(我引用的页面说要把它们放进去),但即使删除分号,也不会发生任何事情……您是否尝试过使用断点来隔离问题的确切根源?是否无法检索收件箱的内容?是否无法移动它们?@originaluser2我使用了一些断点,发现它没有拾取旧路径或新路径,因此我将它们更改为NSStrings(稍后我将添加一个编辑以显示所有新代码)。现在它正确地选择了所有目录名,但实际上并没有将文件从收件箱文件夹移动到文档文件夹。请给出Swift 5的代码。我为此面临很多麻烦。
NSError* error;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
NSLog(@"error: %@", error.localizedDescription);