gpt4 book ai didi

xcode - 尝试将字符串与数组中的元素进行比较时出错

转载 作者:行者123 更新时间:2023-11-30 10:20:33 24 4
gpt4 key购买 nike

    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()
}

}

所以我在这里遇到两个错误(在 checkResponse 中的 if 语句中):首先,“int 没有名为 'subscript' 的成员”,如果我只是在 if 语句中取出对函数的调用,我会得到: “‘字符串’无法转换为‘镜像配置’”

我真的不知道为什么这不起作用。请耐心听我说,因为我是一个 Xcode 菜鸟,只是想在西类牙语方面取得更好的成绩。

最佳答案

非常接近 - 只需要将下标分开:

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

当使用数组数组时(在本例中是 CorrectConjugation ),每个下标都会使您下降一级。

<小时/>

对于另一个问题,您需要几个变量来保存当前动词和前缀索引:

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()
}
}
}

关于xcode - 尝试将字符串与数组中的元素进行比较时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com