Netox
Netox

Reputation: 45

fatal error: Index out of range - Swift 3

I'm trying to send an array which contains countries, so when i pass the data to the second controller i get this error:

fatal error: Index out of range

This is my code

View Controller:

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

What it supposed to do is when i click an element of my list it will send to a second view which contains a MapKit and compare if the index of the array contains is the same as the string, it will locate to that location.
Thank you very much for any kind of help.

Update: I use print(transferPaises.count) and it only show 1 element instead of 10, it isn't sending all elements from the other main view

Upvotes: 0

Views: 684

Answers (1)

Blazej SLEBODA
Blazej SLEBODA

Reputation: 9935

modify each line with if statment to use 0 index

    if (transferPaises[0]=="Rusia") ...
    if (transferPaises[0]=="Alemania") ...
    if (transferPaises[0]=="Japon") ...
...

Upvotes: 0

Related Questions