I have a program that is trying to list my classes for school (in a UITableView), along with the homework for each class. I have variables to track homework for each class as lists (i.e "Englishhw", "Mathhw", etc.), and I want to make it so that in my function, I can take the name of the class, and add "hw" to find the associated variable list. I have all of my homework lists for each class in one View Controller, and based on what the person selected (with the didSelectRowAt function, I can find that), I would show different table view contents for the homework in that class (instead of making a separate view controller for each subject). Here is my code:
@IBOutlet var className: UILabel!
var classList = ["Language", "Math", "English"]
var Languagehw = ["hi", "bye", "New Assignment . . .", "Remove Assignment . . ."]
var Mathhw = ["him", "bye", "New Assignment . . .", "Remove Assignment . . ."]
var Englishhw = ["hime", "bye", "New Assignment . . .", "Remove Assignment . . ."]
func addAssignment(subject: String) {
for subject in classList {
if className.text! == subject {
// subject + hw to find associated variable (ex. subject = English, subject + hw => Englishhw, which is one of my homework lists
}
else {}
}
}
In this code, I'm making a function that checks if the className is equal to a subject in classList (a list with all of the classes) through a for loop and then trying to find the related list by adding the subject + hw. For example, if I wanted to find my "Englishhw" list, I could use the function to find if the class they selected was "English", and then add "hw" at the end to find the list correctly. However, I don't know if there is a way to concatenate strings (in this case, English and hw) to find a variable. Is there actually concatenate strings into variables?