Search code examples
objective-cxcodegoogle-maps-sdk-ios

Why does my application crash when the UITextField text reaches a certain length?


I'm using the Google Places API for iOS. I'm working on creating an autocomplete function, and I'm putting the suggestions in 4 buttons (for reasons that aren't relevant to this question), but I'm noticing that, once the user input reaches a certain length, the application crashes. My code is as follows:

_request = [[NSMutableURLRequest alloc]init];
_escapedUrlString = [_textField.text stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding
    _requestURL = [NSString stringWithFormat: @"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&key=mykey",_escapedUrlString];
[_request setURL: [NSURL URLWithString:_requestURL]];
[_request setHTTPMethod:@"GET"];
NSURLResponse *requestResponse;
_requestHandler = [NSURLConnection sendSynchronousRequest:_request returningResponse:&requestResponse error:nil];
_requestReply = [[NSString alloc]initWithBytes: [_requestHandler bytes] length: [_requestHandler length] encoding: NSASCIIStringEncoding];
_data = [_requestReply dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];
[_pred1 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:0]objectForKey:@"description"] forState: UIControlStateNormal];
[_pred2 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:1]objectForKey:@"description"] forState: UIControlStateNormal];
[_pred3 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:2]objectForKey:@"description"] forState: UIControlStateNormal];
[_pred4 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:3]objectForKey:@"description"] forState: UIControlStateNormal];

In this case, _pred1, _pred2, _pred3, _pred4 are my buttons and _textField is, obviously, the text field where the user input is being entered. This code is within an -(IBEvent)editChange method. The code works and the autocomplete predictions populate the button titles.

My problem is that once I enter a string of a certain length, I receive the following error:

2014-10-07 16:21:48.740 maptest[41319:529100] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

My assumption is that, as the input string gets longer, the number of predictions shorten, so there is nothing to put in the later titles. However, I don't know how to stop the app from crashing once the number of returned predictions decreases.

Any help is appreciated.


Solution

  • You are saying

    [_pred1 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:0]objectForKey:@"description"] forState: UIControlStateNormal];
    [_pred2 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:1]objectForKey:@"description"] forState: UIControlStateNormal];
    [_pred3 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:2]objectForKey:@"description"] forState: UIControlStateNormal];
    [_pred4 setTitle: [[[_json objectForKey:@"predictions"] objectAtIndex:3]objectForKey:@"description"] forState: UIControlStateNormal];
    

    ...without looking first to see whether there is an objectAtIndex:0, an objectAtIndex:1, etc. Look first.