0

I am trying to create a NSMutableArray out of a NSString. It shouldn't be separated by anything. So if the string is @"0123" the array should be {@"0", @"1", @"2", @"3", nil}. But if I use the code bellow it creates an array like {@"0123", nil}. In the code bellow I also convert a regular array to a mutable array, because I don't know how to do it directly..

NSMutableArray *myMutableArray = [[NSMutableArray alloc] initWithArray:[myString componentsSeparatedByString:@""]];
user2142733
  • 71
  • 1
  • 6
  • @H2CO3: I remember few days back, we both solved similar question.. – Anoop Vaidya Apr 10 '13 at 19:42
  • 1
    @AnoopVaidya If you look closely, the dupe is a question from early 2012. (Well, the question what I think you're talking about is a bit different, there the emphasis was on characters, here it's the array that matters... whatever.) –  Apr 10 '13 at 19:43

1 Answers1

2
NSMutableArray *array = [NSMutableArray new];
for (NSInteger i=0; i<string.length; i++) {
    [array addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140