2016-04-28 10 views
-3

こんにちは私は、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) 

どのように私はこの問題を解決するだろうか?ありがとう!

+1

あなたは 'activePlace'に他に何をしますか?あなたはその価値をどのように変えますか?正確にどこがクラッシュしますか?それをデバッグ!ブレークポイントを設定し、 'print'文を追加してください! – luk2302

+0

デバッグが必要だと約束しましたが、私には質問があります。どこで 'activePlaces? 'をインクルードするのですか?あなたのコードを見てみましょう。これは、いつでもあなたがそれにアクセスしようとしているときに、エラーが「範囲外のインデックス」になることを意味します。 – NSGangster

+0

助けてくれてありがとう、これは愚かな質問かもしれませんが、アクティブな場所の値がどのようになっているのですか? –

答えて

0

"Fatal Error: Array Index out of range"?

このエラーは、場所に要求しているインデックスにオブジェクトが含まれていないために発生します。

例:

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

は、この変数で使用しているかどうか確認してください:activePlace

+0

私が 'activePlace'を特集しているのは、私が質問で持っているものだけで、なぜそれが空であるのか理解できません。 –

+0

Man activePlace == -1したがって、-1のインデックスを持つ配列に到達しようとします –

+0

これは場所が追加されていないときだけです –

関連する問題