ios - fatal error : Index out of range - Swift 3

标签 ios swift

我正在尝试发送一个包含国家/地区的数组,因此当我将数据传递给第二个 Controller 时,我收到此错误:

fatal error: Index out of range

这是我的代码

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

第二个 Controller :

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 的第二个 View ,并比较数组包含的索引是否与字符串相同,它将定位到该位置.
非常感谢您提供的任何帮助。

更新: 我使用 print(transferPaises.count) ,它只显示 1 个元素而不是 10 个,它不会从另一个主视图发送所有元素

最佳答案

用 if 语句修改每一行以使用 0 索引

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

关于ios - fatal error : Index out of range - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47367463/

相关文章:

ios - Swift - 通过 API 调用将项目添加到数组

ios - 在 swift 类中使用 Objective-c 属性时的桥接问题

ios - Swift/XCode 编译 Archive 一个非常小的 App 需要 25 分钟

android - 如何将没有 API 和格式的网站抓取到移动应用程序(iPhone、Android 等)中?

ios - 使用自定义标题按钮修改 TableView 中的部分?

objective-c - 如何从 Swift 中的 void UnsafePointer 中提取数据?

ios - 在 Swift 中从 TableViewCell 类切换 View Controller

iphone - 从 iTunes Connect 自动下载销售报告

android - 尝试安装 react-devtools 时出错

ios - 单击后退按钮时,导航堆栈中 VC 的 UINavigationBar 标题颜色不会更改