I am using the following code in XCode 12/Swift 5to try to do the following:
-
Receive a variable passed from one view controller to this one (passedVerb, and this works)
-
Look up items in a dictionary based on the "passedVerb" variable that is passed. This will change depending on which verb the user was on in the main view controller, so I need this to be able to switch it out.
-
When I run the below code in playground it works well
import UIKit var yekol = [0: "yekol", 1: "bekol", 2: "btekol"] var youm = [0: "youm", 1: "boum", 2: "btoum"] var yekol2 = ["inf": "yekol", "ana": "bekol", "enta": "btekol"] var verbs = [yekol, youm] var randVerb = verbs.randomElement() //depending on which random verb is chosen, look up in the right dictionary above var anaForm = randVerb![1]! var entaForm = randVerb![2]! print(anaForm as Any) print(entaForm as Any)
But when I try to do this within the view controller in my main project, I’m not able to used the "passedVerb" in the same way that I could use the "randVerb" to look up in the right dictionary:
import UIKit
class AboutViewController: UIViewController {
//variables
var anaForm = ""
var entaForm = ""
var nehnaForm = ""
//a variable to hold the data from the first ViewController
var passedVerb = ""
let yekol = [0: "yekol",
1: "bekol",
2: "btekol",
3: "mnekol"]
let edros = [0: "edros",
1: "bedros",
2: "btedros",
3: "mnedros"]
func updateTable() {
//below works, but only because I'm explicitly calling the right dictionary
var anaForm = yekol[1]
//neither of the below forms work in the same way as in playground. i get a 'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead. i can't for the life of me figure out how to fix this despite the warning
var entaForm = passedVerb![2]!
var nehnaForm = passedVerb[3]
anaLabel.text = anaForm
entaLabel.text = entaForm
nehnaLabel.text = nehnaForm
}
}
@IBOutlet weak var currentVerb: UILabel!
@IBOutlet weak var anaLabel: UILabel!
@IBOutlet weak var entaLabel: UILabel!
@IBOutlet weak var nehnaLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
currentVerb.text = passedVerb
updateTable()
}
}
Any insight or help would be really appreciated. I’ve been learning Xcode/Swift as my first coding language and so hope this isn’t(maybe is?) too obvious an issue.
Thanks in advance!
2
Answers
you are probably trying to pass a [Int : String]? to passedVerb. But you declare var passedVerb = "" as a String in AboutViewController.
So the code in AboutViewController is trying to find an index into a String, which you cannot do.
Hence the error message you get. Try declaring "passedVerb" like this:
and use it like this:
similarly for "nehnaForm".
If you want help passing a dictionary containing multiple verb forms from one view controller to the next, you need to post your current code that hands the verb to the other view controller.
Are you using a segue and
prepare(for:sender:)
? Add your code that invokes the second view controller and passes the verb.One thing that I notice: In
AboutViewController
you declarepassedVerb
as an emptyString
. However, in your playground code, your verbs are of type[Int:String]
(a dictionary withInt
keys andString
values.You should change the declaration of
passedVerb
to be:Edit:
That is why you are getting the error that you are getting:
You declared your
passedVerb
as a String, and then you try to index into it as if it is a dictionary.You should also stop using
!
force unwrap operators. Those will crash your code if the dictionary doesn’t contain the specified key.