-2

I'm trying to include a method in my application that determines the size of a file such as: file.txt, file.app, file.png, etc. I have general idea of how to do this, tried the following, but my app crashes because of the NSLog function at the end:

NSString *yourPath = @"path/to/file";
NSFileManager *man = [[NSFileManager alloc] init];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
UInt32 result = [attrs fileSize];
NSLog(@"Size: @%", result);
Community
  • 1
  • 1
Joe Habadas
  • 588
  • 7
  • 21
  • 2
    If you posted the "some reason", we could actually help. –  Aug 31 '12 at 18:10
  • Yea, what's the error? *View* > *Debug Area* > *Activate Console* – woz Aug 31 '12 at 18:11
  • 1
    Can you post your answer and mark it as answered? – woz Aug 31 '12 at 18:11
  • 2
    @JoeHabadas it would still be useful to answer your own question so that future visitors with the same problem could benefit from the solution. –  Aug 31 '12 at 18:12
  • It's NSLog causing the crash — feel free to post an answer. I was using %@ instead of %i (but i still think it's not correct), so an answer would be great. – Joe Habadas Aug 31 '12 at 18:13
  • Why do you think it's still not correct? – woz Aug 31 '12 at 18:17
  • It should be UInt32 according to the warning. The app doesn't crash anymore, but straight %i must not be the correct int to use. – Joe Habadas Aug 31 '12 at 18:18

1 Answers1

1

You might want to have a look here NSNumber+Toolbox. You can just use the category .h and .m file and you got a function which gives you a localized file size string. Hope it helps.

Bernd Rabe
  • 800
  • 6
  • 23