0

I have an NSMutableArray that stores values from JSON. I select a specific index from the array to display that value to the button.

That works fine and code that does this work is prepared inside the function.

When I call the function, for example, inside the button it works fine and returns the result I want. But if I call inside viewDidLoad it returns:

** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'

This is the code to display data to the button and my buttons are ten and my array has ten elements:

NSString *onemb =[[mobile firstObject]objectForKey:@"package"];
NSString *onedolar =[[mobile firstObject]objectForKey:@"rate"];
nus_dolar.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
nus_dolar.titleLabel.textAlignment = NSTextAlignmentCenter;
nus_dolar.titleLabel.numberOfLines = 2;
nus_dolar.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
[nus_dolar setTitle:[NSString stringWithFormat:@"%@\n%@", onedolar , onemb ] forState: UIControlStateNormal];

This is my calling function:

[self loadingHomePageData];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user9713861
  • 73
  • 2
  • 7
  • where do you fill your array ? – Arash Etemad Apr 29 '18 at 05:43
  • I fill my array : like this -(void)proxydidFinishLoadingData:(id)data InMethod:(NSString*)method { if ([method isEqualToString:@"getmobiledata"]) { NSMutableDictionary * defaultDict = [[NSMutableDictionary alloc]init]; [defaultDict addEntriesFromDictionary:[NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]]; NSLog(@"%@",defaultDict); [mobile addObjectsFromArray:[[defaultDict objectForKey:@"data"] objectForKey:@"data"]]; } } – user9713861 Apr 29 '18 at 05:47
  • mr arash please guide me – user9713861 Apr 29 '18 at 06:01
  • the error occurred due to you tried to access value at index 1 that doesn't exist. Check if the array is not empty & index is < array.count – Lal Krishna Apr 29 '18 at 06:23
  • my array has ten elements as I mentioned above – user9713861 Apr 29 '18 at 06:27
  • better you create sample project and provide url for the same – Fahim Parkar Apr 29 '18 at 09:10

2 Answers2

0

The function that calls API to get json from server is asynchronous then it takes little time to get the response, and it won't get it in viewDidLoad life cycle.

You can use @escaping completionHandler (in swift- you should convert it to objective-c) to simulate synchronized method for getting response. and then you can use it in viewDidLoad.

see How does a completion handler work on iOS?

Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • I get how it works but still I no idea how to apply my problem by using @escaping completionHandler please guide how to apply to solve the problem I new to iOS. my code is in objective c – user9713861 Apr 29 '18 at 06:26
0

If you fetch your JSON from a server, you can use this

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURL *url = [NSURL URLWithString:@"anUrl"];

[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // your JSON parsing code here
}];
schmidt9
  • 4,436
  • 1
  • 26
  • 32