gpt4 book ai didi

objective-c - 如何在 cocoa 中使用contentsOfDirectoryAtPath从文件夹中获取内容列表?

转载 作者:行者123 更新时间:2023-12-03 17:18:43 36 4
gpt4 key购买 nike

我想获取文件夹的所有内容并添加到 NSMenuItems 的子菜单中。

为了实现这一目标,我使用下面给出的代码:

NSArray *dirContents = [[NSArray alloc]init];
dirContents=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:updated error:nil];

此代码可以运行,但只能在一个文件夹中运行。很奇怪,但这是真的。我已对其他文件夹尝试了相同的代码,但它在 dirContents 中给出了 nil 值。

那么如何访问所选文件夹的所有内容列表?

最佳答案

尝试此来跟踪访问错误。

NSError *error;
NSArray *dirContents = [[NSArray alloc]init];
dirContents=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:updated error:&error];

if(error){
NSLog(@"%@",error);
}

如果应用程序已沙箱化,请确保使用目录的相对路径,而不是绝对路径。

NSURL *containerUrl =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"Your.app.container.id"];
path = [[containerUrl URLByAppendingPathComponent:@"your/folder/path"] path];

关于objective-c - 如何在 cocoa 中使用contentsOfDirectoryAtPath从文件夹中获取内容列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441234/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com