views:

45

answers:

2

So I have a method that takes an NSString as an argument, and I would like that method to basically decompose that sting into individual characters and store them in an array(NSArray).

I other words I would like to read each character in the string and store the individual characters in an array and in the same order, so that I can process the individual characters at a later time.

Any thoughts?

+4  A: 

Iterate through the string, use characterAt - and append each character to an NSMutableArray.

But if your doing that - why bother putting them in the NSArray at all?

NSMutableArray *myArray = [[NSMutableArray] alloc] initWithCapacity:[string length]];

for (i=0;i<[string length];i++) {
  unichaar ch;
  ch = [string  characterAtIndex:i];
  NSLog(@"Processing charachter %c",ch);

   // If you really want
  [myArray addObject:(id) ch];
}
Brad
Yeah good point, I might as well process as I go. Thanks a lot i'll give that a try.
cgossain
corrections: 1) it's `unichar` 2) `[[NSMutableArray alloc] initWithCapacity:` will use the default allocator. the default allocator assumes `id`, and will send retain/release messages to the unichars - -which will of course crash. you have two options: use NSString for each character, or drop down to allocate a CFMutableArray using a custom allocator which does not perform reference counting on the unichars
Justin
@Justin - thanks, yes, your right. The NSArray was an afterthought - because, like I suggested, he shouldn't even bother needing it.
Brad
@Brad yes - i also agree that using/allocating an NSString for each character would kill performance, compared to using a unichar. sometimes that is insignificant. there are also rare cases where an array of single char NSStrings is useful.
Justin
Could also use `-getCharacters:range:` to copy all the chars into a c-style array of unichars, which could be more efficient than extracting characters individually with `-characterAtIndex:`. Still, best solution is probably to process as you go and not use an array at all.
David Gelhar
You shouldn't process a unichar with %c, they can easily be higher than 127.
Jason Coco
The correct escape for unichar is %C
Joshua Weinberg
so iterating works great but I do have a problem, the "processing" I do requires delayed method calls. By delaying method calls inside a loop I noticed that the loop is still runs, instead of waiting for the methods to complete. How can I get the loop to wait for the delayed methods to finish before continuing on to the next iteration?
cgossain
Do these delayed method calls have any facility to tell someone they're done? Like through a selector or delegate function?
Brad
well i'm delaying them using [self performSelector:@(methodCall) withObject:nil afterDelay:0.3];
cgossain
A: 

Besides Brad answer (where he shows you can certainly store each character in a NSArray instance if you want), you also have the more straightforward opportunity to get the NSString value as a plain old C null terminated string. So you can iterate it like this:

char c;
int i = 0;
const char *buffer = [nsstringInstance cStringUsingEncoding:NSASCIIStringEncoding];
while (c = buffer[i++]) {
    // iterating using c char

}
Pablo Santa Cruz
But what if the NSString contains non-ascii characters, or is longer than 1024 characters? Note also that `-getCString:` is deprecated as of iOS2.0.
David Gelhar
@David: true. Changed it for cStringUsingEncoding.
Pablo Santa Cruz