gpt4 book ai didi

ios - 为什么我不能在按钮操作中使用 var 值?

转载 作者:行者123 更新时间:2023-11-30 12:19:56 31 4
gpt4 key购买 nike

我想要将值发送到我的按钮...

为什么我不能在按钮操作中使用 quizWord?

出现“使用未解析的标识符...”错误...

    import UIKit

extension Array
{
/** Randomizes the order of an array's elements. */
mutating func shuffle()
{
for _ in 0..<10
{
sort { (_,_) in arc4random() < arc4random() }
}
}
}
// random




class QuizViewController: UIViewController {



var tasks : [Task] = []
var limitRe:Int = 0
var cards: [Character] = []
var quizName = [String]()
var quizfileName = [String]()

override func viewDidLoad() {
super.viewDidLoad()
//get the data from core data
getData()

for q in 0...tasks.count-1 {
let quiz = tasks[q]
quizName.append(quiz.name!)
quizfileName.append(quiz.filename!)
print(quizName, quizfileName)
}
var quizWord = quizName[0]
var countText: Int = (quizWord.characters.count) - 1

for _ in 0...countText
{let index1 = quizWord.index(quizWord.startIndex, offsetBy: limitRe)

cards.append(quizWord[index1])
limitRe = limitRe + 1
}
cards.shuffle()
quizPhotoView.image = UIImage(named: "1.jpg")

if countText >= 0 {lblChar1.text = "\(cards[0])"} else {lblChar1.text = ""}
if countText >= 1 {lblChar2.text = "\(cards[1])"} else {lblChar2.text = ""}
if countText >= 2 {lblChar3.text = "\(cards[2])"} else {lblChar3.text = ""}
if countText >= 3 {lblChar4.text = "\(cards[3])"} else {lblChar4.text = ""}
if countText >= 4 {lblChar5.text = "\(cards[4])"} else {lblChar5.text = ""}
if countText >= 5 {lblChar6.text = "\(cards[5])"} else {lblChar6.text = ""}
if countText >= 6 {lblChar7.text = "\(cards[6])"} else {lblChar7.text = ""}
if countText >= 7 {lblChar8.text = "\(cards[7])"} else {lblChar8.text = ""}

}



@IBOutlet weak var lblChar1: UILabel!
@IBOutlet weak var lblChar2: UILabel!
@IBOutlet weak var lblChar3: UILabel!
@IBOutlet weak var lblChar4: UILabel!
@IBOutlet weak var lblChar5: UILabel!
@IBOutlet weak var lblChar6: UILabel!
@IBOutlet weak var lblChar7: UILabel!
@IBOutlet weak var lblChar8: UILabel!
@IBOutlet weak var lblTyped: UILabel!
@IBOutlet weak var quizPhotoView: UIImageView!


func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
tasks = try context.fetch(Task.fetchRequest())
}
catch {
print("Failed")
}
}


@IBAction func butChar1(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar1.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar2(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar2.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}
}
@IBAction func butChar3(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar3.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar4(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar4.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar5(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar5.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar6(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar6.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar7(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar7.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}
@IBAction func butChar8(_ sender: UIButton) {
if (lblTyped.text!.characters.count) > (quizWord.characters.count) - 1 {lblTyped.text = ""}
lblTyped.text = lblTyped.text! + lblChar8.text!
if lblTyped.text == quizWord {lblTyped.text = "Good"}}

}

最佳答案

这里您已在 ViewDidLoad 中声明了 quizWord,因此它是一个局部变量,仅在 ViewDidLoad 函数内可用。

如果您希望在整个 viewController QuizViewController 中使用 quizWord,那么您必须在所有函数之外声明它,就像您对 quizfileName 所做的那样>,测验名称等可能就像

   var quizfileName = [String]()
var quizWord:String?

关于ios - 为什么我不能在按钮操作中使用 var 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44918337/

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