1

I have two type of arrays: one for storage the category name:

struct Category {
    var title : String
    var dataArrayName : String
}

var Categories = [
    Category(title: "First Title", dataArrayName: "firstDataArray"),
    Category(title: "Second Title", dataArrayName: "secondDataArray")
]

and another one for storage the category data:

var firstDataArray = [1, 2, 3, 4, 5] // Data Array 1
var secondDataArray = [6, 7, 8, 9, 10] // Data Array 2

Then, in tableView didSelectRowAt indexPath I want to access firstDataArray by dataArrayName from Categories (to push the data array to another View Controller).

I get value of dataArrayName via line Categories[indexPath.row].dataArrayName but I don't know how to access array because I get a String result.

2 Answers 2

2

I think that you could set a "dataArrayName" with the data [Int]. Because, when you set the Array Data Before the categories, you can access this var like this:

struct Category {
    var title : String
    var dataArrayName : [Int]
}

var firstDataArray = [1, 2, 3, 4, 5] // Data Array 1
var secondDataArray = [6, 7, 8, 9, 10] // Data Array 2

var categories = [
    Category(title: "First Title", dataArrayName: firstDataArray),
    Category(title: "Second Title", dataArrayName: secondDataArray)
]
Sign up to request clarification or add additional context in comments.

Comments

1

You probably should access to arrayData not by name, it's very bad practice in programming. Instead of it use instance of Array, like this:

struct Category {
    var title : String
    var dataArray : [Int]
}

var firstDataArray = [1, 2, 3, 4, 5] // Data Array 1
var secondDataArray = [6, 7, 8, 9, 10] // Data Array 2

var categories = [
    Category(title: "First Title", dataArray: firstDataArray),
    Category(title: "Second Title", dataArray: secondDataArray)
]

And finally access to the arrayData:

print(categories[indexPath.row].dataArray)

Comments

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.