Reputation: 631
My code looks like this, sometimes app crashes on the last line, when it tries to log the error. What am I doing wrong?
BOOL isDir;
NSError *error;
NSString *downloadPath = [[NSString stringWithFormat:@"%@/%@", [download downloadFolder], [download escapedTitle]] stringByExpandingTildeInPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:downloadPath isDirectory:&isDir])
{
[fileManager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error)
NSLog(@"%@", [error localizedDescription]);
}
I've also attached the output from the console:
Upvotes: 1
Views: 206
Reputation: 17471
In Cocoa, the NSError **
is only valid if the called method returns an error, which in this case would be if -createDirectoryAtPath:...
returns false.
Instead of testing for if (error)
, test for the return value of the -createDirectoryAtPath:
method being false, and you'll be good to go.
For example:
if (![fileManager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"%@", [error localizedDescription]);
}
Upvotes: 4