我正在尝试发送一个包含国家/地区的数组,因此当我将数据传递给第二个 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/