【发布时间】:2013-08-05 22:55:07
【问题描述】:
我正在尝试将项目从“下载”移动到我的“废纸篓”文件夹,但首先不删除它们。我在下面的实现中缺少什么,文件没有移动。
具体说明:
NSString *trashpath=[NSHomeDirectory() stringByAppendingPathComponent:@".Trash"];
NSString *downloadpath=[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"];
- (void) moveToTrash{
NSError * error = nil;
NSArray * list = [_filemanager contentsOfDirectoryAtPath:_downloadpath error:&error];
//Move items to the trash
for(id obj in list){
NSString *sourcepath = [_downloadpath stringByAppendingPathComponent:obj];
NSString *destpath= [_trashpath stringByAppendingPathComponent:obj];
if([_filemanager moveItemAtPath:sourcepath toPath:destpath error:&error] == YES)
NSLog(@"Moved to trash :%@ ",destpath);
else
NSLog(@"Unable to move %@ ",sourcepath);
}
}
我按照 BergQuester 的建议尝试使用以下代码
if([[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:_downloadpath
destination:@"" files:list tag:nil] == YES)
NSLog(@"Moved to Trash");
else
NSLog(@"Unable to move :%@",[error localizedFailureReason]);
我的输出中出现 Unable to move :(null)。
文件确实在那里,所以我不确定问题仍然存在。
【问题讨论】:
-
@JohnSauer 我会看看这个。
-
@JohnSauer,我想避免遍历每个文件并按照下面 bergquester 的建议单独移动。
标签: objective-c macos nsfilemanager