PDA

View Full Version : How to find out size of a NSString's Contents on Hard Disk?




Armin1983
Feb 19, 2012, 01:14 AM
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.



subsonix
Feb 19, 2012, 06:39 AM
NSFileManager can be used to find out the size of a file, specifically: attributesOfItemAtPath which returns a dictionary that has fileSize information.

Armin1983
Feb 19, 2012, 08:48 AM
NSFileManager can be used to find out the size of a file, specifically: attributesOfItemAtPath which returns a dictionary that has fileSize information.

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.

subsonix
Feb 19, 2012, 09:26 AM
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.

Why?

chown33
Feb 19, 2012, 10:10 AM
The size on disk is going to depend on what encoding you use to write it to disk.

subsonix
Feb 19, 2012, 10:24 AM
The size on disk is going to depend on what encoding you use to write it to disk.

Chances are that this is a solution to a problem that does not exist, since the answer is pretty obvious. RTFM applies. :)

Armin1983
Feb 19, 2012, 11:01 AM
The size on disk is going to depend on what encoding you use to write it to disk.
encoding:NSUTF8StringEncoding

JoshDC
Feb 19, 2012, 02:03 PM
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.

Armin1983
Feb 19, 2012, 11:19 PM
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.

Thanks for guidance,

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

gnasher729
Feb 20, 2012, 03:03 AM
Thanks for guidance,

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

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.

chown33
Feb 20, 2012, 09:34 AM
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.

The XY Problem (http://www.perlmonks.org/index.pl?node_id=542341)
.. 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.

whooleytoo
Feb 20, 2012, 09:44 AM
The XY Problem (http://www.perlmonks.org/index.pl?node_id=542341)
.. 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.

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.

;)