【发布时间】:2014-07-20 08:54:09
【问题描述】:
我正在使用以下代码检索位于文档目录中的 PDF 文件的文件大小。
- (NSString *)sizeOfFile:(NSString *)filePath {
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSInteger fileSize = [[fileAttributes objectForKey:NSFileSize] integerValue];
NSString *fileSizeString = [NSByteCountFormatter stringFromByteCount:fileSize countStyle:NSByteCountFormatterCountStyleFile];
return fileSizeString;
}
NSURL *url = [self.arrayOfBooks objectAtIndex:indexPath.row];
NSLog(@"%@",[self sizeOfFile:url.absoluteString]); // Showing zero KB.
我确定arrayBooks 中有文件,因为它可以显示在cellForRowAtIndexPath 中。
但我不知道为什么文件大小显示为零。
请帮帮我。
【问题讨论】:
-
您是否验证过
filePath确实存在?您是否确认fileAttributes不是nil?使用error参数到attributesOfItemAtPath:error:。 -
fileAttributes 显示为零。但是filePath是正确的。
-
再次,使用
error参数查看为什么fileAttributes是nil。 -
错误信息是 .错误域=NSCocoaErrorDomain 代码=260“操作无法完成。(Cocoa 错误 260。)”UserInfo=0x10934b640 {NSFilePath=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.1- 64/Applications/08BE9071-6251-44ED-A8E0-55CD478380FC/Documents/myPDFFile.pdf, NSUnderlyingError=0x10934b170 "操作无法完成。没有这样的文件或目录"}
标签: ios nsurl nsfilemanager