Search code examples
iphoneiphone-sdk-3.0ios4

How would I decompose an NSString into individual characters?


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?


Solution

  • 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];
    }