使用segue传递struct数组中的值:index超出范围

时间:2016-05-11 10:00:10

标签: ios arrays struct xcode7 segue

希望这是我需要问的最后一个问题! 我现在已经调查了48个小时,但我仍然找不到答案。

以下是我正在使用的代码:

DataSource.swift:

struct Game {
    var name : String
    var cheats : [Cheat]
}
struct Cheat {
    var name : String
    var code : String
    var desc : String
}

GameListViewController.swift

import Foundation
import UIKit

class GameListViewController: UITableViewController {

    var gamesArray = [Game]()
    var cheatsArray = [Cheat]()

    override func viewDidLoad() {
        super.viewDidLoad()

        gamesArray = [Game(name: "Game1", cheats: [Cheat(name: "cheat1", code: "code1", desc: "desc1")])]

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
        cell.textLabel?.text = gamesArray[indexPath.row].name
        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let DestViewController = segue.destinationViewController as! CheatListViewController

        var DataPass : Cheat
        DataPass = cheatsArray[indexPath.row]
        DestViewController.cheatnameArray = DataPass.name

        var DataPass2 : Cheat
        DataPass2 = cheatsArray[indexPath.row]
        DestViewController.cheatcodeArray = DataPass2.code

        var DataPass3 : Cheat
        DataPass3 = cheatsArray[indexPath.row]
        DestViewController.cheatdescArray = DataPass3.desc
    }

}

CheatListViewController.swift

class CheatListViewController: UITableViewController {

    var cheatcodeArray = String()
    var cheatnameArray = String()
    var cheatdescArray = String()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

当我从gamesArray中选择“游戏1”时,我立即从第一个“DataPass”实例收到索引超出范围错误。 我已经以这种方式构建了我的数据源,因此我不必单独编辑数组并保持我的对象整洁。

如果有人能指出我正确的方向,我将永远感激不尽!

亲切的问候 罗里

1 个答案:

答案 0 :(得分:1)

对我来说,看起来你没有用任何作弊填充你的cheatsArray变量。这就是为什么你收到超出范围异常的索引。

从您的代码中了解您想要达到的目标有点难,但我想我已经拥有它......

注意我使用可选绑定来打开destinationViewController,这是安全的,因为执行的任何其他segue也会触发相同的prepareForSegue

if let destViewController = segue.destinationViewController as? CheatListViewController {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    var game = gamesArray[indexPath.row]

    destViewController.cheatnameArray = game.cheats.map({ $0.name })
    destViewController.cheatcodeArray = game.cheats.map({ $0.code })
    destViewController.cheatdescArray = game.cheats.map({ $0.desc })
}

将数组更改为实际的字符串数组而不是字符串..

var cheatcodeArray = [String]()
var cheatnameArray = [String]()
var cheatdescArray = [String]()

The Basics