致命错误:尝试到达位置时,数组索引超出范围

时间:2016-01-03 08:13:29

标签: xcode swift

我有一个应用,用户可以通过不同的方式添加位置。 一种是通过在地图上录制而另一种是通过点击按钮来获取其位置。 所有这些都存储在tableview中,用户可以按下tableView中的位置返回到地图上的位置。 第一部分效果很好但是当我尝试使用按钮时,该地方保存到tableView(女巫是完美的)但是当我点击它回到保存的地方时,应用程序崩溃并且我得到了致命错误:数组索引超出范围"。我一直试图找到一个解决方案,但仍然没有发现如何。这是代码,如果您需要更多帮助我,请告诉我。

请注意我的活动位置不是空的,因为这是我在调试区域中得到的:

activePlaces current value is 1
activePlaces current value is 1
activePlaces current value is (activePlaces)
activePlaces current value is -1
[<+37.33131185,-122.03075659> +/- 30.00m (speed 7.87 mps / course 176.86) @ 6/8/16, 6:54:38 PM Central European Summer Time]
[<+37.33131185,-122.03075659> +/- 30.00m (speed 7.87 mps / course 176.86) @ 6/8/16, 6:54:38 PM Central European Summer Time]
[<+37.33131185,-122.03075659> +/- 30.00m (speed 7.87 mps / course 176.86) @ 6/8/16, 6:54:38 PM Central European Summer Time]
[<+37.33115792,-122.03076853> +/- 30.00m (speed 8.33 mps / course 178.34) @ 6/8/16, 6:54:38 PM Central European Summer Time]
[<+37.33115792,-122.03076853> +/- 30.00m (speed 8.33 mps / course 178.34) @ 6/8/16, 6:54:38 PM Central European Summer Time]
[<+37.33107980,-122.03077250> +/- 30.00m (speed 8.20 mps / course 177.87) @ 6/8/16, 6:54:39 PM Central European Summer Time]
[<+37.33100231,-122.03077622> +/- 30.00m (speed 8.20 mps / course 178.34) @ 6/8/16, 6:54:40 PM Central European Summer Time]
[<+37.33092521,-122.03077056> +/- 10.00m (speed 8.34 mps / course 173.92) @ 6/8/16, 6:54:41 PM Central European Summer Time]
[<+37.33085898,-122.03075862> +/- 10.00m (speed 7.23 mps / course 170.25) @ 6/8/16, 6:54:42 PM Central European Summer Time]
[<+37.33079800,-122.03072881> +/- 10.00m (speed 7.11 mps / course 157.66) @ 6/8/16, 6:54:43 PM Central European Summer Time]
[<+37.33074557,-122.03069850> +/- 10.00m (speed 6.29 mps / course 155.33) @ 6/8/16, 6:54:44 PM Central European Summer Time]
[<+37.33069642,-122.03066881> +/- 10.00m (speed 5.94 mps / course 156.32) @ 6/8/16, 6:54:45 PM Central European Summer Time]
[<+37.33065643,-122.03064682> +/- 10.00m (speed 4.77 mps / course 157.35) @ 6/8/16, 6:54:46 PM Central European Summer Time]
[<+37.33063636,-122.03063618> +/- 10.00m (speed 2.27 mps / course 158.84) @ 6/8/16, 6:54:47 PM Central European Summer Time]
[<+37.33061331,-122.03062365> +/- 10.00m (speed 1.90 mps / course 159.97) @ 6/8/16, 6:54:48 PM Central European Summer Time]
[<+37.33058184,-122.03060705> +/- 10.00m (speed 2.83 mps / course 160.58) @ 6/8/16, 6:54:49 PM Central European Summer Time]
activePlaces current value is 2
activePlaces current value is 2
fatal error: Index out of range
(lldb) 

在tableView中:

var places = [Dictionary<String,String>()]

var activePlace = -1
   alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

        let lat = places[indexPath]["lat"]!

        let lon = places[indexPath]["lon"]!

        places.removeAtIndex(indexPath)

        places.insert(["name" : alert.textFields![0].text!, "lat" : lat, "lon" : lon], atIndex: indexPath)

        self.tableView.reloadData()

        NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
        NSUserDefaults.standardUserDefaults().synchronize()

我也有这个,仍然在tableView

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

        activePlace = indexPath.row

        return indexPath

    }
ViewController中的

@IBAction func addCurentLoc(sender: UIBarButtonItem) {

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

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

    //title = "new address"

    let annotation = MKPointAnnotation();
    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;

    }


    if activePlace == -1 {

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

    } else {

print(&#34; activePlaces当前值为(activePlace)&#34;)

        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)
    }
}

错误发生在

let latitude = NSString(string:places [activePlace] [&#34; lat&#34;]!)。doubleValue

女巫说&#34; EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)&#34;

0 个答案:

没有答案
相关问题