Posted on by | Posted in Mobile Blog, Tutorial | Tagged

Have you ever pondered on the NSString class reference and you were overwhelmed by the sheer amount of methods? I certainly have and due to the endless possibilities those methods give you, I just couldn't figure out how to split an NSString by characters and create a NSArray of those characters.

Blame it on my lack of creativity or on the fact that I'm not a C buff. However, thanks to my creative Googling, I came across this site, which shows you how to do it:

NSMutableArray *characters = 
[[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
    NSString *ichar  = 
[NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
    [characters addObject:ichar];
}

Although Apple's documentation is extensive, you sometimes need a little hint or guidance to achieve your goal.



One Response to Split NSString by characters

  1. Aish says:

    Thankyou! finally i found out how to do it


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.