将目录移至垃圾箱

nuk*_*ukl 1 cocoa objective-c

我需要将目录(包括其内容)移动到垃圾箱.我NSWorkspaceRecycleOperation在文档中找到了,并编写了这段代码:

NSString *path = [NSString stringWithString:@"/Users/test/Desktop/test"];

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                                             source:path 
                                        destination:@"" 
                                              files:dirContents 
                                                tag:nil];
Run Code Online (Sandbox Code Playgroud)

它将所有内容移动到垃圾箱,而不是目录本身.那么,我该怎么做呢?

Geo*_*che 9

您目前只对目录内容执行循环操作.给定一个dir垃圾邮件目录,请使用以下内容:

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation 
                               source:[dir stringByDeletingLastPathComponent]
                               destination:@"" 
                               files:[NSArray arrayWithObject:[dir lastPathComponent]]
                               tag:nil];
Run Code Online (Sandbox Code Playgroud)