Indexing 索引超出范围-9223372036854775807-Swift 3

Indexing 索引超出范围-9223372036854775807-Swift 3,indexing,swift3,tableview,ios10,indexoutofrangeexception,Indexing,Swift3,Tableview,Ios10,Indexoutofrangeexception,我有一个字符串数组,其中的位置显示在表中,当用户选择表单元格时,该位置将添加到另一个数组中。我想通过第二个数组在标签中显示选定的位置。但是,当我选择了太多的位置或列表中较低的位置时,会出现以下错误: 致命错误:索引超出范围 当我选择每个位置时,所有位置的索引号都是9223372036854775807。如何让代码识别每个位置的索引号 @IBOutlet weak var tableLocations: UITableView! let itemsLocations: [String] = ["P

我有一个字符串数组,其中的位置显示在表中,当用户选择表单元格时,该位置将添加到另一个数组中。我想通过第二个数组在标签中显示选定的位置。但是,当我选择了太多的位置或列表中较低的位置时,会出现以下错误:

致命错误:索引超出范围

当我选择每个位置时,所有位置的索引号都是9223372036854775807。如何让代码识别每个位置的索引号

@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]
var locationsPreviewtext = [String]()

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellLoco: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellLocations")!
    cellLoco.textLabel?.text = itemsLocations[indexPath.row]
    return cellLoco
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    locationsPreviewtext.append(itemsLocations[indexPath.row]) //I get the error here when I select
    labelLocationsPreview.text = "\(locationsPreviewtext)"
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if locationsPreviewtext.isEmpty {
        return
    }
    else {
        let ind = index(ofAccessibilityElement: locationsPreviewtext[indexPath.row]) //I get the error here when I deselect
        if ind == NSNotFound {
            locationsPreviewtext.remove(at: ind)
        }
        labelLocationsPreview.text = "\(locationsPreviewtext)"
    }
}

@IBOutlet weak var labelLocationsPreview: UILabel!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destDetails: ViewController = segue.destination as! ViewController
    destDetails.labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"
}

我们看不到您的代码,如何回答您的问题?问题已编辑。感谢您愿意提供帮助。您的
locationsPreviewtext
itemsLoactions
没有相同的索引,因为您构建它的方式不同。您的错误发生在
didrowat
方法上。谢谢!我该如何着手解决这个问题?我对Swift非常陌生。我们没有看到您的代码,我们怎么可能回答您的问题?问题已编辑。感谢您愿意提供帮助。您的
locationsPreviewtext
itemsLoactions
没有相同的索引,因为您构建它的方式不同。您的错误发生在
didrowat
方法上。谢谢!我该如何着手解决这个问题?我对斯威夫特非常陌生。