-1

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.

JayB127
  • 73
  • 9
  • you are pointing to a non existing element, make sure that object for key @"predictions" has a valid value at x position – J Sanchez Oct 07 '14 at 20:44
  • I attempted to add a condition checking if the object == nil and only changing the text if it wasn't, but I'm still getting the error. – JayB127 Oct 07 '14 at 20:53
  • Why do you put the JSON dictionary in a variable named `json` but then used another variable named `_json` to set the button titles? And why do you call `[[_json objectForKey:@"predictions"]` four times? Call it once and save the result in a variable. Then you can check its length and reuse the variable. – rmaddy Oct 07 '14 at 22:02

1 Answers1

0

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.

matt
  • 447,615
  • 74
  • 748
  • 977
  • I attempted to add a condition checking if the object == nil and only changing the text if it wasn't, but I'm still getting the error. – JayB127 Oct 07 '14 at 20:50
  • It isn't whether the object is nil. It can never be nil (you can't put nil in an NSArray). It's whether the index count even goes that high. You can't speak of the object at index `1` in an array that doesn't go beyond index `0`. – matt Oct 07 '14 at 22:47
  • Here are the NSArray docs: https://developer.apple.com/library/IOS/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/index.html – matt Oct 07 '14 at 22:49
  • I'm not sure what I was thinking there. I got it cleaned up now and it's working. thank you. – JayB127 Oct 08 '14 at 00:52
  • Well, there are languages, such as Ruby, where asking for a non-existent index on an array will return nil. But this is Objective-C, and it isn't one of those languages. :( – matt Oct 08 '14 at 02:11