elise
elise

Reputation: 3

errors while trying to compare a string to element in array

    let verbList: [String] = ["hacer", "ser", "estar"]
let POVList: [String] = ["él / usted","ella / usted","ellas / ustedes","ellos / ustedes","tú","yo","nosotros",]
let correctConjugation: [[String]] = [["hace","hace","hacen","hacen","haces","hago","hacemos"], ["es","es","son","son","eres","soy","somos"], ["está","está","estan","estan","estas","estoy","estamos"]]


func randomVerb() -> Int {                          //creates and returns a random number for the prefix arrray
    var randomVerb = Int(arc4random_uniform(3))
    return randomVerb
}

func randomPrefix() -> Int {                        //creates and returns a random number for the verb array
    var randomPrefix = Int(arc4random_uniform(7))
    return randomPrefix
}


@IBAction func changeVerb(sender: AnyObject) {

    Verb.text = verbList[randomVerb()]
    POV.text = POVList[randomPrefix()]

    userResponse.backgroundColor = UIColor.whiteColor()
    userResponse.text = ""


}


@IBAction func checkResponse(sender: AnyObject) {

    var userResponseA: String
    userResponseA = userResponse.text
    if (userResponseA == correctConjugation[randomVerb()[randomPrefix()]]){
        userResponse.backgroundColor = UIColor.greenColor()
    } else {
        userResponse.backgroundColor = UIColor.redColor()
    }

}

So I get two errors here (in the if statement in checkResponse): first, "int does not have a member named 'subscript'" and if I just take out the call for the function in the if statement I get: "'String' is not convertible to 'Mirror Disposition'"

I really have no idea why this is not working. Bear with me, as I am an Xcode noob just trying to get a better grade in spanish.

Upvotes: 0

Views: 57

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

Very close - just need to have your subscripts separated:

if (userResponseA == correctConjugation[randomVerb()][randomPrefix()]) {
    // ...
}

When working with an array of arrays (in this case correctConjugation), each subscript takes you one level down.


For the other issue, you want a couple variables to hold the current verb and prefix indexes:

class VC: UIViewController {
    // list declarations here
    var verbIndex = 0
    var povIndex = 0

    @IBAction func changeVerb(sender: AnyObject) {
        verbIndex = randomVerb()
        povIndex = randomPrefix()

        Verb.text = verbList[verbIndex]
        POV.text = POVList[povIndex]

        userResponse.backgroundColor = UIColor.whiteColor()
        userResponse.text = ""
    }

    @IBAction func checkResponse(sender: AnyObject) {
        var userResponseA = userResponse.text

        if (userResponseA == correctConjugation[verbIndex][povIndex]){
            userResponse.backgroundColor = UIColor.greenColor()
        } else {
            userResponse.backgroundColor = UIColor.redColor()
        }
    }
}

Upvotes: 1

Related Questions