NSFileManager 错误?

发布于 2024-11-29 03:13:29 字数 325 浏览 0 评论 0原文

你能发现这行代码中的错误吗?它返回零! 该应用程序完全沙盒化,但启用了下载文件夹访问。

NSArray*array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"] error:NULL];
        //array==nil: Why?

[编辑]问题:我无法测试它。它发生在评论机器的来宾帐户上。编译的二进制文件是否有问题,或者您有任何解决此问题的提示吗?

Can you find a bug in this line of code? It returns nil!!
The app is completely sandboxed but Downloads folder access is enabled.

NSArray*array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"] error:NULL];
        //array==nil: Why?

[EDIT] Problem: I cannot test it. It is happening on a Guest account of the review machines. Could there be something wrong with the complied binary or have you got any tips how to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风流物 2024-12-06 03:13:29

尝试读取带有错误句柄的目录来检查发生了什么:

NSError *error = nil;
NSArray*array = [[NSFileManager defaultManager] 
                  contentsOfDirectoryAtPath:
                   [NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"] 
                                      error:&error];
if ( !array ) 
     NSLog(@"ERROR: %@", [error description]);

这将为您提供更详细的描述出了什么问题。

要将此错误记录到文件quick'n'dirty,请使用以下消息:

[[error description] writeToFile:@"strangeerrors.log" 
  atomically:NO encoding:NSUTF8StringEncoding error:nil];

Try to read the directory with an error handle to examine what happens:

NSError *error = nil;
NSArray*array = [[NSFileManager defaultManager] 
                  contentsOfDirectoryAtPath:
                   [NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"] 
                                      error:&error];
if ( !array ) 
     NSLog(@"ERROR: %@", [error description]);

That will give you a more detailed description what went wrong.

To log this error to a file quick'n'dirty use the following message:

[[error description] writeToFile:@"strangeerrors.log" 
  atomically:NO encoding:NSUTF8StringEncoding error:nil];
烂柯人 2024-12-06 03:13:29

您是否尝试过替换

[NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"]

[@"~/Downloads" stringByExpandingTildeInPath]

Have you tried replacing

[NSHomeDirectory() stringByAppendingPathComponent:@"/Downloads/"]

with

[@"~/Downloads" stringByExpandingTildeInPath]

?

深海不蓝 2024-12-06 03:13:29

附加路径组件时不需要添加任何斜线。很简单:

[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"] ...

对我来说效果很好!

You don't need to put in any slashed when appending path components. It's simply:

[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"] ...

Works fine for me!

不打扰别人 2024-12-06 03:13:29

几乎可以肯定这不是真正的问题,但可以说将 @"Downloads/" 附加为路径组件比使用前导斜杠更有意义。在 Cocoa 看来,请求 @"/Downloads/" 的组件将为您提供:

@"/"
@"Downloads"

这可能不是您想要的。

Almost certainly not the real problem here, but arguably it makes more sense to append @"Downloads/" as a path component, rather than with a leading slash. In Cocoa's eyes, asking for the components of @"/Downloads/" will give you:

@"/"
@"Downloads"

which is probably not what you intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文