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
  • 22,070
  • 5
  • 50
  • 76
Pop Flamingo
  • 2,741
  • 1
  • 23
  • 59

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,007
  • 12
  • 22
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,642
  • 3
  • 25
  • 30