2017-11-18 14 views
0

私は国を含む配列を送信しようとしているので、私は2番目のコントローラにデータを渡すとき、私はこのエラーを取得:致命的なエラー:範囲外のインデックス - スウィフト3

fatal error: Index out of range 

これは私ですコード

ビューコントローラ:

let paises = [ "Mexico","Alemania","Rusia","Japon","Australia", 
       "Texas","Peru","Argentina","Chile","Italia"] 
    var myIndex = 0 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
       return paises.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier: "cell") 
     cell.textLabel?.text = paises[indexPath.row] 
     return(cell) 

    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     myIndex = indexPath.row 
     performSegue(withIdentifier: "segue", sender: self) 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let secondController = segue.destination as! SecondController 
     secondController.transferPaises = [paises[myIndex]] 
    } 

SecondController

import UIKit 
import MapKit 

class SecondController: UIViewController { 

    var transferPaises = Array<String>() 
    var location = CLLocationCoordinate2D() 
    let span = MKCoordinateSpanMake(0.002, 0.002) 


    @IBOutlet weak var map: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if (transferPaises[0]=="Mexico") { 

      location.latitude = 19.432608 
      location.longitude = -99.133209 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[1]=="Alemania") { 

      location.latitude = 51.1642 
      location.longitude = 10.4541 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[2]=="Rusia") { 

      location.latitude = 55.751244 
      location.longitude = 37.618423 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[3]=="Japon") { 

      location.latitude = 35.685360 
      location.longitude = 139.753372 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[4]=="Australia") { 

      location.latitude = -33.856159 
      location.longitude = 151.215256 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[5]=="Texas") { 

      location.latitude = 29.890661 
      location.longitude = -97.911530 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 
     if (transferPaises[6]=="Peru") { 

      location.latitude = -12.046374 
      location.longitude = -77.042793 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[7]=="Argentina") { 

      location.latitude = -34.603722 
      location.longitude = -58.381592 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[8]=="Chile") { 

      location.latitude = -33.447487 
      location.longitude = -70.673676 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 

     if (transferPaises[9]=="Italia") { 

      location.latitude = 41.890251 
      location.longitude = 12.492373 
      let region = MKCoordinateRegion(center: location, span: span) 

      map.setRegion(region, animated: true) 

     } 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

私のリストの要素をクリックすると、MapKitを含む2番目のビューに送信され、配列のインデックスに文字列と同じものがあるかどうかが比較されますその場所に。
ご協力いただきありがとうございます。

更新: 私はプリント(transferPaises.count)を使用し、それは1つのだけの要素の代わりに10を示し、それがなステートメント場合と各行を変更他のメインビューから

答えて

0

のすべての要素を送信していません0のインデックスを使用する

+0

フォーク作業です! – Netox

+0

非常に感謝! – Netox

関連する問題