Re: NSMutableString, NSString ,character for character
Re: NSMutableString, NSString ,character for character
- Subject: Re: NSMutableString, NSString ,character for character
- From: jgo <email@hidden>
- Date: Thu, 2 Aug 2001 16:14:54 -0700
>
Ali Ozer <email@hidden> Thu, 2001-07-12 19:39:03 -0700
>
There is indeed characterAtIndex:. Loop from 0 to [string length] - 1.
>
...
>
One question is, why do you want the individual characters?
>
Typically people don't need to go into strings too much.
>
>
Now for a slightly advanced, performance topic:
>
>
Instead of characterAtIndex:, you can also use getCharacters:range: for
>
bulk getting of characters. If you're enumerating all the characters in
>
a potentially huge string and doing something quick with each one,
>
calling getCharacters:range: a few times into a fixed size stack
>
allocated buffer might be quicker than many calls to characterAtIndex:
>
as it will avoid numerous message sends. (If you decide to use the
>
bulk-access method, also see CFStringGetCharacterFromInlineBuffer() in
>
CFString.h.)
Some cases come to mind. (Sorry for rambling.)
The storage is always dynamic and potentially huge. Suppose you
want to give some arbitrary, discontiguous set of characters a
custom style, draw a box around them, around each, give them a
scalloped under-line, give the under-line a different color from
the associated text, whatever (just trying to mention some
possibilities that aren't done for us by the frameworks).
Maybe I've run a syntax parser and it tells me that this sub-string
has this conceptual purpose or that and I want to show that with
color (the easy case that is at least partly done by the frameworks)
or some other weirdness.
It might be that I'm letting a user set such characteristics for
selected text, and then need to apply them during a later session
(i.e. when they are not selected). It could be that I decide to
apply such a custom style to every nth character in the doc.
Oh, and most of the time I want to get back to the same characters
whether others have been inserted before, after, or within a run
to which the custom style applies.
Well, I can cache such information (the indices or ranges of the
affected characters), re-parse every time any of it might need
to draw itself, split the text up into separate objects for each
run and keep information on how the different runs fit together...
Anyway, all the program knows at this arbitrary point in the
process is that the nth character or the glyph for the nth character
needs to be treated a certain way.
The other thing that's puzzling in this regard is that "index" is
used in at least two senses. It can "point to" the nth character
in the document or run or string or..., or it can be a code point,
the index into, e.g. the Unicode table. How does a "range" differ
from a pair of indices?
And lastly, when will all those "Description forthcoming"s disappear
from the text handling and other Cocoa doc web pages? A year ago
would be good. :B-)
John G. Otto Nisus Software, Engineering
www.infoclick.com www.mathhelp.com www.nisus.com software4usa.com
EasyAlarms PowerSleuth NisusEMail NisusWriter MailKeeper QUED/M
Will program Macs for food.