开发者

NSFileManager Bug?

开发者 https://www.devze.com 2023-03-27 20:20 出处:网络
Can you find a bug in this l开发者_开发百科ine of code? It returns nil!! The app is completely sandboxed but Downloads folder access is enabled.

Can you find a bug in this l开发者_开发百科ine 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?


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];


Have you tried replacing

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

with

[@"~/Downloads" stringByExpandingTildeInPath]

?


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

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

Works fine for me!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号