开发者

How would I decompose an NSString into individual characters?

开发者 https://www.devze.com 2023-01-22 23:14 出处:网络
So I have a method that takes an NSString as an argument, and I would like that method to basically decompose that sting into indivi开发者_如何学Godual characters and store them in an array(NSArray).

So I have a method that takes an NSString as an argument, and I would like that method to basically decompose that sting into indivi开发者_如何学Godual 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?


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


I’m not normally this judgmental, but the current answers will work… until they don’t!

There is only a single way in ObjC/Foundation to enumerate actual Unicode characters:

- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block

using the option

NSStringEnumerationByComposedCharacterSequences

The problem here is best illustrated using Emoji:

0

精彩评论

暂无评论...
验证码 换一张
取 消