-1

I am retrieving file size of PDF file that located in document directory with following codes.

- (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.

I am sure there is file in arrayBooks because it can show in cellForRowAtIndexPath.

But i don't know why file size is showing zero.

Please help me.

Fire Fist
  • 7,032
  • 12
  • 63
  • 109
  • Have you verified that `filePath` actually exists? Have you verified that `fileAttributes` isn't `nil`? Use the `error` parameter to `attributesOfItemAtPath:error:`. – rmaddy May 30 '14 at 14:44
  • fileAttributes is showing nil. But filePath is correct. – Fire Fist May 30 '14 at 14:47
  • Again, use the `error` parameter to see why `fileAttributes` is `nil`. – rmaddy May 30 '14 at 14:48
  • Error message is . Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 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 "The operation couldn’t be completed. No such file or directory"} – Fire Fist May 30 '14 at 14:50

1 Answers1

1

The most likely cause of your issue is due to an invalid path being sent to sizeOfFile:.

Your code:

NSURL *url = [self.arrayOfBooks objectAtIndex:indexPath.row];
NSLog(@"%@",[self sizeOfFile:url.absoluteString]); // Showing zero KB.

should be:

NSURL *url = [self.arrayOfBooks objectAtIndex:indexPath.row];
NSLog(@"%@",[self sizeOfFile:[url path]]);

You want to call the path method to get a file path. absoluteString returns a file URL, not a path.

Also, don't use property syntax to call methods.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks you. Now i got. I don't understand don't use property syntax to call method.Why i shouldn't use it.Please could you explain? – Fire Fist May 30 '14 at 14:57