为什么我得到一个崩溃说"致命错误:数组索引超出范围"?

时间:2016-04-28 14:05:35

标签: xcode swift

您好我正在尝试在mapView中添加一个点的应用程序,将其发送到ViewController,当用户点击tableView中的某个位置时,它应该发送回mapView中的点,但这是我收到错误,我的应用程序崩溃,我得到"线程1 exc_bad_instruction(code = exc_1386_invop subcode = 0x0)"在线上说:

    let latitude = NSString(string: places[activePlace]["lat"]!).doubleValue

这是我的代码

if activePlace == -1 {


        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

            } else {



    let latitude = NSString(string: places[activePlace]["lat"]!).doubleValue

    let longitude = NSString(string: places[activePlace]["lon"]!).doubleValue

    let coordinate = CLLocationCoordinate2DMake(latitude, longitude)

    let latDelta:CLLocationDegrees = 0.01

    let lonDelta:CLLocationDegrees = 0.01

    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

    let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)

    self.Map.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()

    annotation.coordinate = coordinate

    annotation.title = places[activePlace]["name"]

    self.Map.addAnnotation(annotation)






    //save data start

    NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places") // saves data to NSUserDefaults

    //stop save data

}

以下是“活跃的地方”#'

var activePlace = -1



 override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

        activePlace = indexPath.row

        return indexPath

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "newPlace" {

            activePlace = -1


        }



@IBAction func addCurentLoc(sender: UIBarButtonItem) {

        var newCoordinate2 = self.Map.userLocation.location!.coordinate;

        var location = CLLocation(latitude: newCoordinate2.latitude, longitude: newCoordinate2.longitude)

        //title = "new address"

        //try change order start

        let annotation = MKPointAnnotation()

        self.Map.addAnnotation(annotation)

        annotation.coordinate = newCoordinate2

        annotation.title = title


        annotation.coordinate = self.Map.userLocation.location!.coordinate;

        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) -> Void in


            var title = ""

            if (error == nil) {

                if let p = placemarks?[0] {

                    var subThouroughfare:String = ""
                    var thouroughfare:String = ""

                    if p.subThoroughfare != nil {

                        subThouroughfare = p.subThoroughfare!

                    }

                    if p.thoroughfare != nil {

                        thouroughfare = p.thoroughfare!

                    }

                    title = "\(subThouroughfare) \(thouroughfare)"


                }

            }

            if title == "" {

                title = "Added \(NSDate())"

            }

            places.append(["name":title,"lat":"\(newCoordinate2.latitude)","lon":"\(newCoordinate2.longitude)"])

            let annotation = MKPointAnnotation()

            annotation.coordinate = newCoordinate2

            annotation.title = title

            self.Map.addAnnotation(annotation)

        }

        self.Map.addAnnotation(annotation);


        func mapView(mapView: MKMapView!,
                     viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!{
            if(annotation is MKUserLocation){
                return nil;
            }

            // let pinView: Void = mapView.addAnnotation(annotation);
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier:"MyIdentifier");
            return pinAnnotationView;

        }






    }

印刷声明说:

fatal error: Index out of range
(lldb) 

我该如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

  

"致命错误:数组索引超出范围"?

由于地方不包含您要求的索引上的任何对象,因此会发生此错误。

示例:

    var places = [0,1,2,4] 
    // next line is crash because array do not contains any item on 5 index
    places[5]

尝试检查您在此变量中使用的内容:activePlace