Re: bytesize of an object
Re: bytesize of an object
- Subject: Re: bytesize of an object
- From: Ken Thomases <email@hidden>
- Date: Fri, 25 Jan 2008 10:38:15 -0600
On Jan 25, 2008, at 5:29 AM, Hans van der Meer wrote:
I extended NSData with a category in order to implement NSData
payload compression, using zlib. That works fine for data for which
I know its length, but seems more difficult in case of objects.
Like for example NSString's, in which I am especially interested in
compressing them.
I could think of two procedures:
(1) using the archiver to pack the NSString into a NSData object
from which I can retrieve the payload and then compress it
(2) dump the NSString contents into an array with getCharacters,
compress that array and then put it inside a NSData object
However, both procedures need a (seemingly unnecessary) extra step
I would like to avoid. If it were possible to find the bytesize of
objects, I could do the compression directly on it.
My question thus: is it possible to retrieve the actual bytesize of
an object? Or is there another solution to the above compression
problem which I am overlooking?
I think you're seeking something which isn't sensible to seek, in two
ways. First, there's no reason to believe that all of an object's
data is stored contiguously in memory. For an NSString, the
character data is almost certainly not "in" the object. It's most
likely in memory elsewhere and a pointer to that memory is in the
NSString. So, even if you found out the bytesize of the object, it
wouldn't include the character data.
Second, an NSString's character data is in no particular form, as far
as clients of the class are concerned. The character data might be
stored internally as UTF-16. Or, it might be stored as UTF-8. Or,
it might be in any one of a number of other internal formats, even
some Apple-custom format. In all probability, different string
objects will keep their character data in different forms. Big
strings, or mutable strings, might not even keep it all in one
buffer. The point is, you neither know nor (should) care.
Therefore, it's impossible to know if it's suitable for compressing
in its internal form. (Perhaps it's already compressed!)
It only makes sense to consider compressing the data in a known
external format. So, you'd need to use one of the methods which
return the character data in a specified encoding. getCharacters: is
suitable. So would be dataUsingEncoding: or cStringUsingEncoding:, etc.
Which basically gets you back to the solutions you're already aware of.
-Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden