How to find out size of a NSString's Contents on Hard Disk?

Discussion in 'Mac Programming' started by Armin1983, Feb 18, 2012.

  1. macrumors newbie

    Joined:
    Jan 29, 2012
    #1
    Hello guys,

    Imagine, I have a NSString object, inside it, there are strings, i want to know how size it is on disk if i save the content of file on computer as a file.
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    NSFileManager can be used to find out the size of a file, specifically: attributesOfItemAtPath which returns a dictionary that has fileSize information.
     
  3. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #3
    As i said, i want to know the size of the future file before i save the content of NSString to a file on my disk.
     
  4. subsonix, Feb 19, 2012
    Last edited: Feb 19, 2012

    macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    Why?
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    The size on disk is going to depend on what encoding you use to write it to disk.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Chances are that this is a solution to a problem that does not exist, since the answer is pretty obvious. RTFM applies. :)
     
  7. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #7
    Code:
    encoding:NSUTF8StringEncoding
     
  8. macrumors regular

    Joined:
    Apr 8, 2009
    #8
    This page from CocoaDev answers the question thoroughly:

    http://www.cocoadev.com/index.pl?FileSize

    Short answer: you can use NSString's –lengthOfBytesUsingEncoding: to get the number of bytes, but the actual size of that file on disk will dependant on other factors. As others have said, it will be useful to know why you need this information so we can suggest a suitable solution.
     
  9. thread starter macrumors newbie

    Joined:
    Jan 29, 2012
    #9
    Thanks for guidance,

    my nsstring object is loaded by html contents. how would be the solution in this case?
     
  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    What makes you think there is a difference? Asked before, but what is it actually that you try to achieve? Usually, after people have tried for ages answering your questions, it then turns out that you asked the completely wrong questions in the first place.
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    The XY Problem
    .. You want to do X, and you think Y is the best way of doing it.
    .. Instead of asking about X, you ask about Y.
     
  12. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #12
    The best thing about this forum is that people don't just simply answer the question, but offer advice on the method & approach taken.

    The worst thing about this forum, is that people don't just simply answer the question, but offer advice on the method & approach taken.

    ;)
     

Share This Page