-1

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?

Swaraag
  • 85
  • 11
  • I haven't been learning Swift for too long, so I'm not sure what you mean... what do you mean by using objects, not arrays? – Swaraag Oct 09 '21 at 17:46
  • Variable names are fictitious concept invented solely for developer convenience. They don't actually exist within the program after compilation. It looks like you're looking for a [`Dictionary`](https://developer.apple.com/documentation/swift/dictionary). – Alexander Oct 09 '21 at 18:21
  • @Swaraag the duplicate link might answer your particular question, however, as others have said, you are looking at the problem from the wrong angle. – Cristik Oct 09 '21 at 18:46
  • @Cristik I made it a dictionary and had the key be the class and the values being the homework in a list. When making the textlabel the indexPath.row, it gives me a "Fatal error: Index out of range" error. `cell.textLabel?.text = subjecthw["spanhw"]?[indexPath.row]`. Even when I make a variable for that specific key and value, it gives me the same error. `var spanhw = subjecthw["spanhw"]` `cell.textLabel?.text = spanhw?[indexPath.row]` – Swaraag Oct 11 '21 at 03:20
  • You’re trying to access an index larger than the number of elements in the array. For example, if the array has 2 elements and you’re tying to access the item at index 5 – jnpdx Oct 11 '21 at 03:57
  • `"spanhw": ["hi", "bye", "New Assignment . . .", "Remove Assignment . . ."]` is the spanhw part of my dictionary. It has 4 values, but I made it ` return 10` in numberOfRowsInSection – Swaraag Oct 11 '21 at 04:05
  • Oh just realized what you're saying. That makes a lot more sense! Thanks! – Swaraag Oct 11 '21 at 04:06

2 Answers2

0

Forget concatenation; it's irrelevant.

What you need to know is that Swift has no introspection. There is no way to get from the string "Language" to the value of the variable Language (or Languagehw or anything else); that is a metaphysical impossibility.

You need to rethink your entire architecture (which is pretty bad already: you should be using objects, not arrays). If you need to access a particular value by way of a string, that would be a dictionary with string keys.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    This is more of a comment. If I were OP, I wouldn't know where to go from here. – Alexander Oct 09 '21 at 18:22
  • @Alexander Gosh, I think I've answered the question; what the OP wants to do (access the value of the variable `Languagehw` by forming the string `"Languagehw"`) is impossible. Done. – matt Oct 09 '21 at 18:30
  • 2
    To add to @Alexander's comment, this question was asked a lot of times before, let's give the man the duplicate reference, and guide him in the comments if we want to make the world a better place – Cristik Oct 09 '21 at 18:32
  • "You can't do that" without a suggestion of "but you can try this instead" has pretty minuscule utility. – Alexander Oct 09 '21 at 18:34
  • I totally agree about the duplicate. The problem is that the question was wrongly closed as a duplicate of questions about string concatenation. I reopened it _exactly_ so that we could replace that with a duplicate about string access to variables. – matt Oct 09 '21 at 18:39
  • The discussion here is about the answer, not the question, if you were sure the question was a duplicate, albeit of a different one, then the best course of actions would've been to just reopen it and leave a comment. – Cristik Oct 10 '21 at 05:53
-1

Inside your for loop, you can write it as-

if className.text! == subject + "hw" { 
      // here subject + "hw" is Englishhw now.
}

We can easily concate string as- after the given string mystring + "hw" before given string "hw" + mystring

Imran0001
  • 580
  • 4
  • 11
  • How would you be able to access that string then? Say I wanted to add a new assignment to that specific Englishhw list in this function, how could I do that? – Swaraag Oct 09 '21 at 17:08