0

I want to create a NSArray with every character for a NSString, the problem is that when I use componentsSeparatedByString:@"" to get every single character in my array, but I am actually getting the whole string in one single case... why ?

Larme
  • 24,190
  • 6
  • 51
  • 81
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64

2 Answers2

1

Something like this might work too.

    NSString *stringToSplit = @"1234567890";
    NSMutableArray *arrayOfCharacters = [NSMutableArray new];
    [stringToSplit enumerateSubstringsInRange:NSMakeRange(0, stringToSplit.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [arrayOfCharacters addObject:substring];
    }];
lead_the_zeppelin
  • 2,017
  • 13
  • 23
0

Because your string doesn't have a string @"" this is an empty string. What you want to do is separate the characters like so:

NSMutableArray *characters = [[NSMutableArray alloc] init];

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

Characters will now contain each letter.

CW0007007
  • 5,681
  • 4
  • 26
  • 31