0
我想使用NSFileManager將文件從Documents/Inbox文件夾移動到Documents文件夾。我用斷點來確定我的代碼不工作,並且一切似乎都運行良好,直到它碰到實際移動文件的部分。找到目錄並在調試器中記錄這些文件,但它不會移動。這裏是我的整個viewDidLoad方法(有一個在[super viewDidLoad];
什麼):文件沒有被移動使用NSFileManager
//Turn every file inside the directory into an array
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appFolderPath = [path objectAtIndex:0];
NSString *inboxAppFolderPath = [appFolderPath stringByAppendingString:@"/Inbox"]; //changes the directory address to make it for the inbox
//NSPredicates to filter out files I don't want in my NSArray
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];
//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];
}
要看兩件事:moveItemAtPath返回一個布爾值,你檢查了它的返回值嗎?另外,你有沒有嘗試傳遞一個NSError對象(而不是零)來查看可能發生的錯誤? – Thunk
我添加了一個NSError,並將文件移動到BOOL success = [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];然後添加了一個if語句來檢查錯誤。我收到錯誤「錯誤消除路徑:(null)」,就是這樣。 – JustMe
你可以在moveItemAtPath調用之前馬上NSLog記錄'oldPath'和'newPath'的值,並在問題中增加一些例子嗎?有時候我們可以看到我們正在處理的是什麼。 – Thunk