こんにちは私は、MapViewのポイントを追加するアプリケーションをしようとしている、それをViewControllerに送信し、ユーザーがtableViewの場所をタップすると、mapViewのポイントに戻って送信する必要がありますが、これは私ですここ 「致命的なエラー:アレイインデックスが範囲外です」というクラッシュが発生するのはなぜですか?
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
}
と私のコードです:言う私のアプリがクラッシュし、エラーを取得し、私が手に「スレッド1 EXC_BAD_INSTRUCTION(コード= exc_1386_invopサブコード= 0x0の)」の行にどのようにアクティブな場所が「作られたか」
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;
}
}
print文はこれを言う:
fatal error: Index out of range
(lldb)
どのように私はこの問題を解決するだろうか?ありがとう!
あなたは 'activePlace'に他に何をしますか?あなたはその価値をどのように変えますか?正確にどこがクラッシュしますか?それをデバッグ!ブレークポイントを設定し、 'print'文を追加してください! – luk2302
デバッグが必要だと約束しましたが、私には質問があります。どこで 'activePlaces? 'をインクルードするのですか?あなたのコードを見てみましょう。これは、いつでもあなたがそれにアクセスしようとしているときに、エラーが「範囲外のインデックス」になることを意味します。 – NSGangster
助けてくれてありがとう、これは愚かな質問かもしれませんが、アクティブな場所の値がどのようになっているのですか? –