我是 Objective-C 的新手,所以请原谅我缺乏知识。我这里有一段代码,我似乎无法正常工作。我想做的是在单击按钮时显示一个目录选择面板。一旦用户选择一个目录,我想创建一个包含该目录中所有内容的数组。最终我想使用这个数组将子目录和文件(用户选择的目录中的所有内容)列表复制到另一个位置。
我收到一条警告,提示找不到实例方法“-contentsofdirectoryaturl:options:error”(返回类型默认为 id)。我不太确定这意味着什么或如何解决它,我怀疑这是我的问题。提供的任何建议都会很棒。谢谢!
- (IBAction)selectfiles:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanChooseFiles:NO];
[openPanel setAllowsMultipleSelection:NO];
if ( [openPanel runModal] == NSOKButton ) {
NSArray *accountPath = [openPanel URLs];
NSLog (@"%@", accountPath);
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *contents;
contents = [filemgr contentsOfDirectoryAtURL:accountPath options:(NSDirectoryEnumerationSkipsHiddenFiles) error:nil];
}
}
contentsOfDirectoryAtURL:
有一个额外的参数includingPropertiesForKeys:
你已经省略了。这就是编译器警告您的原因。该参数是您想要预取的属性列表。在最简单的情况下,您可以指定一个空数组。
另一个错误是[openPanel URLs]
返回一个arrayURL 的数量,即使只选择了一项。
所以你的代码应该是这样的:
NSURL *accountPath = [[openPanel URLs] objectAtIndex:0];
NSLog (@"%@", accountPath);
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *contents;
contents = [filemgr contentsOfDirectoryAtURL:accountPath
includingPropertiesForKeys:[NSArray array]
options:(NSDirectoryEnumerationSkipsHiddenFiles)
error:nil];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)