0

Hopefully this will be the last question i need to ask! I have been looking into this for 48 hours now and i still cannot find answers.

Here is the code i am using:

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()
    }

}

When i select "Game 1" from gamesArray i instantly receive an index out of range error from the first instance of "DataPass". I have structured my datasource in this way so that i do not have to edit arrays separately and keep my objects neat and tidy.

If someone could point me in the right direction i would be forever grateful !

Kind regards Rory

3
  • What is the value of indexPath.row ? What is the value of cheatsArray.count ? Commented May 11, 2016 at 10:12
  • If i understand correctly, cheatsArray.count should be 1 as their is an instance of [Cheat] in my gamesArray.. Or am i totally misunderstanding the way in which struct - arrays work? Commented May 11, 2016 at 10:21
  • I can't find any place in your code where you add a cheat to your cheatsArray, this has to be done manually.. like you do with gamesArray.. Commented May 11, 2016 at 10:23

1 Answer 1

1

For me it looks like you haven't populated your cheatsArray variable with any cheats. That's why you receive an index out of range exception.

From your code it is a bit hard to understand what you're looking to achieve, but I think I have it..

Notice I use an optional binding to unwrap the destinationViewController, this is safe because any other segue performed will also trigger the same 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 })
}

Change your arrays to actual string arrays and not strings..

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

The Basics

Sign up to request clarification or add additional context in comments.

3 Comments

" Cannot convert value of type '[String]' to closure result type 'String' "
Sorry Laffen, it did work, i just forgot to reverse the changes i made before your answer!! Awesome stuff thank you so much :)
NP, glad it helped =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.