gpt4 book ai didi

ios - 使用 segue : index out of range 从结构数组传递值

转载 作者:行者123 更新时间:2023-11-29 00:59:43 25 4
gpt4 key购买 nike

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

这是我使用的代码:

数据源.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”的第一个实例的索引超出范围错误。我以这种方式构建了我的数据源,这样我就不必单独编辑数组并保持我的对象整洁。

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

亲切的问候罗里

最佳答案

对我来说,您似乎没有用任何作弊填充您的 cheatsArray 变量。这就是您收到索引超出范围异常的原因。

从你的代码中很难理解你想要实现的目标,但我想我已经做到了..

请注意,我使用了一个可选的绑定(bind)来打开 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

关于ios - 使用 segue : index out of range 从结构数组传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37159162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com