简体   繁体   English

异常“ NSRangeException”,原因:“ ***-[__ NSArray0 objectAtIndex:]:空NSArray的索引0超出范围”

[英]exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

usually this code has worked perfectly for me, up until recently I went to update my project to support the latest iOS and when clicking on the map button, receive the error; 通常,这段代码对我来说是完美的,直到最近我去更新项目以支持最新的iOS,并且在单击地图按钮时收到错误;

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

Prior to using Xcode 9 it would work correctly, so I am guessing the way the code works is slightly different now. 在使用Xcode 9之前,它可以正常工作,所以我猜现在代码的工作方式略有不同。

The array is NSArray *arr_OfCountryCode; 数组是NSArray *arr_OfCountryCode;

And we initialise it in the viewDidLoad 然后在viewDidLoad初始化它

//Initialize NSArray object with Images

arr_OfCountryCode=@[@"AF",@"AL",@"DZ",@"AS",@"AD",@"AO",@"AI",@"AQ",@"AG",@"AR",@"AM",@"AW",@"AC",@"AU",@"AT",@"AZ",@"BS",@"BH",@"BD",@"BB",@"BY",@"BE",@"BZ",@"BJ",@"BM",@"BT",@"BO",@"BA",@"BW",@"BV",@"BR",@"IO",@"VG",@"BN",@"BG",@"BF",@"BI",@"KH",@"CM",@"CA",@"IC",@",CV",@"BQ",@"KY",@"CF",@"EA",@"TD",@"CL",@"CN",@"CX",@"CP",@"CC",@"CO",@"KM",@"CD",@"CG",@"CK",@"CR",@"HR",@"CU",@"CW",@"CY",@"CZ",@"CI",@"DK",@"DG",@"DJ",@"DM",@"DO",@"EC",@"EG",@"SV",@"GQ",@"ER",@"EE",@"ET",@"FK",@"FO",@"FJ",@"FI",@"FR",@"FG",@"PF",@"TF",@"GA",@"GM",@"GE",@"DE",@"GH",@"GI",@"GR",@"GL",@"GD",@"GP",@"GU",@"GT",@"GG",@"GN",@"GW",@"GY",@"HT",@"HM",@"HN",@"HK",@"HU",@"IS",@"IN",@"ID",@"IR",@"IQ",@"IE",@"IM",@"IL",@"IT",@"JM",@"JP",@"JE",@"JO",@"KZ",@"KE",@"KI",@"XK",@"KW",@"KG",@"LA",@"LV",@"LB",@"LS",@"LR",@"LY",@"LI",@"LT",@"LU",@"MO",@"MK",@"MG",@"MW",@"MY",@"MV",@"ML",@"MT",@"MH",@"MQ",@"MR",@"MU",@"YT",@"MX",@"FM",@"MD",@"MC",@"MN",@"ME",@"MS",@"MA",@"MZ",@"MM",@"NA",@"NR",@"NP",@"NL",@"NC",@"NZ",@"NI",@"NE",@"NG",@"NU",@"NF",@"KP",@"MP",@"NO",@"OM",@"PK",@"PW",@"PS",@"PA",@"PG",@"PY",@"PE",@"PH",@"PN",@"PL",@"PT",@"PR",@"QA",@"RO",@"RU",@"RW",@"RE",@"WS",@"SM",@"SA",@"SN",@"RS",@"SC",@"SL",@"SG",@"SX",@"SK",@"SI",@"SB",@"SO",@"ZA",@"GS",@"KR",@"SS",@"ES",@"LK",@"BL",@"SH",@"KN",@"LC",@"MF",@"PM",@"VC",@"SD",@"SR",@"SJ",@"SZ",@"SE",@"CH",@"SY",@"ST",@"TW",@"TJ",@"TZ",@"TH",@"TL",@"TG",@"TK",@"TO",@"TT",@"TA",@"TN",@"TR",@"TM",@"TC",@"TV",@"UM",@"VI",@"UG",@"UA",@"AE",@"GB",@"US",@"UY",@"UZ",@"VU",@"VA",@"VE",@"VN",@"WF",@"EH",@"YE",@"ZM",@"ZW",@"AX"];

When the user taps the button, a location on the map is shown at random, within the actionMethod to do that is; 当用户点击按钮时,将在actionMethod内随机显示地图上的某个位置;即,

str_Type = @"Randum";
        self.mapView.hidden = NO;
        int randomIndex = arc4random() % [arr_OfCountryCode count];
        NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",[arr_OfCountryCode objectAtIndex:randomIndex]];

        NSURL *url = [[NSURL alloc] initWithString:urlString];

I am struggling as to why the array is classed as empty when it worked for multiple iOS versions and devices prior to the latest. 对于数组在最新版本之前适用于多个iOS版本和设备的原因,我感到很困惑。

It is showing as nil as soon as I check it in Xcode, other arrays initialise before it with no issue, I even tried copying an existing array, renaming it to *arr_OfCountryCode; 当我在Xcode中检查它时,它显示为nil ,其他数组初始化之前没有问题,我什至尝试复制一个现有数组,将其重命名为*arr_OfCountryCode; and that still showed as nil ! 而且仍然显示为nil

In this method (entire posted below) I believe the problem is occuring. 用这种方法(下面有整个文章),我相信问题正在发生。

- (IBAction)actionMethod:(UIButton *)sender {
    if ([sender tag] == 0) {
        self.musicView.hidden = NO;
        [self.tbl_MusicTable reloadData];
    } else if ([sender tag] == 1) {
        str_Type = @"Randum";
        self.mapView.hidden = NO;
        int randomIndex = arc4random() % [arr_OfCountryCode count];
        NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",[arr_OfCountryCode objectAtIndex:randomIndex]];

        NSURL *url = [[NSURL alloc] initWithString:urlString];

        [NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

            if (error) {
                NSLog(@"%@",error.description);
            } else {

                NSDictionary *jsonObject=[NSJSONSerialization
                                          JSONObjectWithData:data
                                          options:NSJSONReadingMutableLeaves
                                          error:nil];

                NSDictionary *places = [[[[jsonObject objectForKey:@"results"] objectAtIndex:0] objectForKey:@"geometry"] objectForKey:@"location"];
                NSDictionary *locations = [[[jsonObject objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"];

                float lat = [[places objectForKey:@"lat"] floatValue];
                float lon = [[places objectForKey:@"lng"] floatValue];

                MKCoordinateRegion region;
                MKCoordinateSpan span;
                span.latitudeDelta = 0.50;
                span.longitudeDelta = 0.50;

                location.latitude = lat;
                location.longitude = lon;
                region.span = span;
                region.center = location;
                [self.mk_MapView setRegion:region animated:YES];

                NSString *str_Title = [[locations valueForKey:@"long_name"] objectAtIndex:0];
                NSString *str_Short = [[locations valueForKey:@"short_name"] objectAtIndex:0];

                MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                point.coordinate = location;
                point.title = str_Title;
                point.subtitle = str_Short;
                [self.mk_MapView addAnnotation:point];




            }
        }];
    }
}

The further code for corelocation is here 进一步的核心定位代码在这里

#pragma mark - CLLocationManager Delegate Method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if ([str_Type isEqualToString:@"Custom"]) {
        CLLocation *currentLocation = newLocation;
        NSString *str_Longitude;
        NSString *str_Latitude;
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta = 0.020;
        span.longitudeDelta = 0.020;
        location.latitude = currentLocation.coordinate.latitude;
        location.longitude = currentLocation.coordinate.longitude;
        region.span = span;
        region.center = location;
        [self.mk_MapView setRegion:region animated:YES];

        if (currentLocation != nil) {
            str_Longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            str_Latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }

        // Reverse Geocoding
        [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error == nil && [placemarks count] > 0) {
                placemark = [placemarks lastObject];
                MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                point.coordinate = currentLocation.coordinate;
                point.title = placemark.country;
                point.subtitle = placemark.locality;
                [self.mk_MapView addAnnotation:point];
            } else {
                NSLog(@"%@", error.debugDescription);
            }
        } ];
    } else {
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = location;
        point.title = placemark.country;
        point.subtitle = placemark.locality;
        [self.mk_MapView addAnnotation:point];
    }
}

#pragma mark - MKMapView Delegate Methods
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pinView = nil;
    if(annotation != self.mk_MapView.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[self.mk_MapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        //pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.canShowCallout = YES;
        //pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"HesOnThisMap"];    //
    }
    else {
        //[self.mk_MapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    MKAnnotationView *aV;
    for (aV in views) {
        CGRect endFrame = aV.frame;
        aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height);
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.45];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [aV setFrame:endFrame];
        [UIView commitAnimations];
    }
}

这与Google Maps api回调有关,当达到最大回调时崩溃

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范围” - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' ***由于未捕获的异常&#39;NSRangeException&#39;而终止应用程序,原因:&#39;***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范围 - *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray ***由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范围” - *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' 由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:空NSArray的索引2超出范围 - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray [__NSArray0 objectAtIndex:]:空NSArray的索引0超出范围 - [__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray NSRangeException与NSArray objectAtIndex,索引超出范围 - NSRangeException with NSArray objectAtIndex, index beyond bounds 由于未捕获的异常&#39;NSRangeException&#39;而终止应用程序,原因:&#39;***-[__ NSArray0 objectAtIndex:] - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:] NSArray objectAtIndex索引0超出空数组的边界 - NSArray objectAtIndex index 0 beyond bounds for empty array NSRangeException。 对于空NSArray索引0超出范围 - NSRangeException. For empty NSArray index 0 beyond bounds NSRangeException&#39;,原因:&#39;*** -[__NSArrayM objectAtIndex:]:索引 5 超出空数组的边界&#39; - NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM