0

Im using the following piece of code to add elements to array dynamically.

for (response in jsonDic[@"value"][@"options"]){
                 NSMutableArray *notifyText = [[NSMutableArray alloc]init];
                 [notifyText  addObject: jsonDic[@"value"][@"options"][response]];
                 NSLog(@"it is%@",notifyText[1]);
             }

When i try to access using notifyText[1] ,what is the logic that I'm missing?

1
  • 1
    Can you show me your response? Commented Feb 20, 2016 at 14:17

3 Answers 3

1

you have create the notifyText Array every time so it is every time alloc and add only one value

please Do like

 NSMutableArray *notifyText = [[NSMutableArray alloc]init];
for (response in jsonDic[@"value"][@"options"]){
                 [notifyText  addObject: jsonDic[@"value"][@"options"][response]];
                 }
 NSLog(@"it is%@",notifyText[1]);
Sign up to request clarification or add additional context in comments.

Comments

0

Indices in array start with 0. First element has index=0. Try

 NSLog(@"it is%@",notifyText[0]);

Comments

0

you are allocating MutableArray named "notifyText" inside for loop,which is being allocated every time ,for last value it is initialized and object is added at 0 index and you are trying to fetch from index 1,that is why app is getting crashed.Do one thing alloc array above the for loop or in ViewDidLoad method.

Comments

Your Answer

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