我的代码看起来像这样,有时应用程序在最后一行崩溃,当它尝试记录错误时。我做错了什么?
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]);
}
我还附加了控制台的输出:
答案 0 :(得分:4)
在Cocoa中,NSError **
仅在被调用方法返回错误时才有效,在这种情况下,如果-createDirectoryAtPath:...
返回false,则该错误。
不是测试if (error)
,而是测试-createDirectoryAtPath:
方法的返回值是否为假,你会很高兴。
例如:
if (![fileManager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"%@", [error localizedDescription]);
}