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 ?
Asked
Active
Viewed 205 times
0

Larme
- 24,190
- 6
- 51
- 81

Pop Flamingo
- 3,023
- 2
- 26
- 64
2 Answers
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
-
Thank you ! Will it also work for non ASCII characters ? – Pop Flamingo Apr 23 '14 at 13:21
-
Believe so as it's just iterating over characters. If this works mark as answer so that other people know. :-) – CW0007007 Apr 23 '14 at 13:21
-
Yes I'll do that as soon as possible ! Thank you ! – Pop Flamingo Apr 23 '14 at 13:22
-
No problems. Any issues just let me know .. – CW0007007 Apr 23 '14 at 13:24
-
It really worked ! Apparently there isn't any problem, but there is something that I am finding strange, in the table, all the ASCII characters are of type _NSCFConstantString* while non ASCII characters (like "ö") are _NSCFCString* is it normal ? – Pop Flamingo Apr 23 '14 at 13:36
-
See: http://stackoverflow.com/questions/10220683/what-is-the-different-between-nscfstring-and-nsconstantstring – CW0007007 Apr 23 '14 at 13:58