Re: NSString or NSData
Re: NSString or NSData
- Subject: Re: NSString or NSData
- From: Fritz Anderson <email@hidden>
- Date: Thu, 22 Jul 2004 11:35:37 -0500
Your instinct is correct that NSData and NSMutableData are the right
tools for handling streams of bytes. NSData is a class that represents
chunks of bytes in their literal representation, as they might come
from a socket or go into a file. NSString represents an interpretation
of a chunk of bytes as a series of characters; depending on the
encoding scheme, the mapping between bytes and characters need not be
simple (UTF-8 characters are between one and four bytes long). NSString
also knows about things like comparisons, format conversions, and
substitutions, that depend on the properties of characters, as distinct
from bytes.
Because NSString stores data in interpreted form, you should treat data
flow to NSString as one-way until proven otherwise. You cannot move an
arbitrary buffer to an NSString and back out, and guarantee an
identical result. Use NSData when you care about preserving the
original bytes, or aim to produce a specific byte sequence for the wire
or a file.
RTF counts as data that behaves like a string; the string
representation and the binary representation can be interchanged
without loss of meaning (so long as you know what string encoding was
used for the binary representation). An image is an example of data
that should be left in NSData.
The down side of sticking to NSData is that you're thrown back to the
standard C library for things like searches.
For conversion from NSData to NSString: You have noticed NSString's
initWith
Data:encoding: method?
Also, you should look at NSAttributedString and
NSMutableAttributedString. These are classes that do what you seem to
intend with strings -- add and manipulate colors and other attributes
-- and they can produce and be initialized from RTF. See
-[NSAttributedString RTFFromRange:documentAttributes:] for example. In
your place, I wouldn't bother with perfecting my own RTF generator,
when I had a library framework already in place to specify my string
and its colors, and get correct RTF for free.
-- F
On 21 Jul 2004, at 6:17 PM, The Baur Family wrote:
I'm writing a program that will collect the payloads of TCP packets on
the wire and (among other things) reconstruct them. Right now, I just
want the text of the transaction, but eventually I may want to be able
to save off as an image, or some other (arbitrary) file.
I started off using NSString internally to store the data, since I was
inserting RTF commands to format the text with colors to denote
client/server info. Now I'm moving to not inserting the RTF until
displayed, so I can do other manipulations on it, if needed.
The problem I'm running into is that I've been using NSMutableString
methods to replace occurrences of strings and inserting RTF commands
as simple @"" constructs. It seems that NSData gives me less
flexibility to be able to modify the contents before displyaing it,
but I don't want to cause problems if I'm storing binary data in an
NSString. Also, I don't see offhand how to convert NSData back into a
NSString.
What route is the best for this type of application?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.