【发布时间】:2016-01-17 04:47:26
【问题描述】:
我有一个 iOS 应用程序,并且我已经在我的应用程序中为 .plist 文件实现了“打开方式”功能。一切正常,除了当我导入 .plist 文件时,它们最终出现在文档/收件箱中,而不是在常规文档文件夹中。有没有办法将其更改为显示在文档文件夹而不是收件箱文件夹中,或者移动它们?我目前正在使用以下代码,但它似乎没有做任何事情。代码来自this页面的第三个回复。
//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
使用此代码,应用程序可以正确查看目录并告诉我收件箱文件夹中的文件,但实际上并没有将其内容传输到 Documents 文件夹。
【问题讨论】:
-
为什么
stringWithFormat:@";%@/Inbox"中有分号? -
@originaluser2 我不太确定(我引用的页面说要放入它们),但即使删除分号,也没有任何反应......
-
您是否尝试过使用断点来隔离问题的确切根源?是否无法检索收件箱的内容?是不是无法移动它们?
-
@originaluser2 我使用了一些断点,发现它没有选择 oldPath 或 newPath,所以我将它们更改为 NSStrings(稍后我将添加一个编辑以显示所有新的代码)。现在它会正确提取所有目录名称,但实际上并没有将文件从 Inbox 文件夹移动到 Documents 文件夹。
标签: ios objective-c directory