0

I have an array named as child_list like child_list: "9,8,21,22,20,24,25", take it in NSString at index 0 when I convert it into string its work but when i use NSMutableArray it crashed, I don't know why.

My code is here:

if([theDictionary objectForKey:@"child_list"])
    {
        NSString *str = [[theDictionary objectForKey:@"child_list"]objectAtIndex:0];
        NSLog(@"Child List %@",str);
        _child_list = [[NSMutableArray alloc ]initWithObjects: [str componentsSeparatedByString:@","], nil];
    }

When break point come on str it show result when it comes on _child_list it crashed.

3
  • does NSLog(@"Child List %@",str); this log prints "9," . or just "9" Commented Apr 23, 2015 at 4:48
  • Create child_list as NSMutableArray. it will work perfectly. no need to change anything. Commented Apr 23, 2015 at 4:56
  • how about trying like that: _child_list = [str componentsSeparatedByString:@","].mutableCopy Commented Apr 23, 2015 at 5:06

2 Answers 2

1

Use initWithArray method while allocating _child_list NSMutableArray

 if([theDictionary objectForKey:@"child_list"])
    {
        NSString *str = [[theDictionary objectForKey:@"child_list"]objectAtIndex:0];
        NSLog(@"Child List %@",str);
        _child_list = [[NSMutableArray alloc ] initWithArray:[str componentsSeparatedByString:@","]];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What's the , nil for at the end? arrayWithArray: only takes one parameter. And my not simply use mutableCopy on the array?
@bhavesh taking arraywitharray like your code its occur an error
0

Please share what your log returns and the dictionary you used.. The code [str componentsSeparatedByString:@","] returns an Array. So, when you're allocating a mutable array, you're creating array of array... Instead using initWithObjects, use initWithArray method and pass [str componentsSeparatedByString:@","] to it.

2 Comments

like that _child_list = [[NSMutableArray alloc ]initWithArray: [str componentsSeparatedByString:@","], nil];
No, use it this way.. _child_list = [[NSMutableArray alloc ]initWithArray: [str componentsSeparatedByString:@","]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.