为什么我的代码没有显示附加到整数的字符串值?

时间:2016-02-13 23:47:01

标签: swift

我对编程很陌生,Swift是我学习的第一门语言。

我一直在研究这个问题,但我并不确定我是否正在研究正确的事情。到目前为止,我所有的研究都是关于将整数转换为字符串等,但我怀疑我是在咆哮错误的树。也许我的问题标题也有误导性,但这是我的情景。

我正在开发一个测验应用程序,它将所有问题和答案存储在.json文件中,格式如下:

{
    "id" : "1",
    "question": "Earth is a:",
         "answers": [
        "Planet",
        "Meteor",
        "Star",
        "Asteroid"
      ],
      "difficulty": "1"
  }

正确答案是始终。在.json文件中首先列出的答案。

该应用程序使用四个可能的答案按钮(使用以下代码进行了洗牌)提出问题:

    func loadQuestions(index : Int)
    {
        let entry : NSDictionary = allEntries.objectAtIndex(index) as! NSDictionary
        let question : NSString = entry.objectForKey("question") as! NSString
        let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray

        //println(question)
        //println(arr)

        labelQuestion.text = question as String

        let indices : [Int] = [0,1,2,3]
        //let newSequence = shuffle(indices)
        let newSequence = indices.shuffle()
        var i : Int = 0
        for(i = 0; i < newSequence.count; i++)
        {
            let index = newSequence[i]
            if(index == 0)
            {
                // we need to store the correct answer index
                currentCorrectAnswerIndex =  i

            }

            let answer = arr.objectAtIndex(index) as! NSString
            switch(i)
            {
            case 0:
                buttonA.setTitle(answer as String, forState: UIControlState.Normal)
                break;

            case 1:
                buttonB.setTitle(answer as String, forState: UIControlState.Normal)
                break;

            case 2:
                buttonC.setTitle(answer as String, forState: UIControlState.Normal)
                break;

            case 3:
                buttonD.setTitle(answer as String, forState: UIControlState.Normal)
                break;

            default:
                break;
            }
}

按下按钮后,应用程序将使用以下代码检查所选答案:

func checkAnswer( answerNumber : Int)
{
    if(answerNumber == currentCorrectAnswerIndex)
    {
        // we have the correct answer
        labelFeedback.text = "Correct!"
        labelFeedback.textColor = UIColor.greenColor()
    score = score + 1
    labelScore.text = "Score: \(score)"

    totalquestionsasked = totalquestionsasked + 1
    labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

    accumulatedquestionsasked = accumulatedquestionsasked + 1

    percentagecorrect = score / totalquestionsasked

    SaveScore()
    SaveBestScore()

    // later we want to play a "correct" sound effect
    PlaySoundCorrect()

}
else
{
    // we have the wrong answer
    labelFeedback.text = "Wrong!"
    labelFeedback.textColor = UIColor.blackColor()

    totalquestionsasked = totalquestionsasked + 1
    labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"

    accumulatedquestionsasked = accumulatedquestionsasked + 1

    SaveScore()
    SaveBestScore()

    // we want to play a "incorrect" sound effect
    PlaySoundWrong()
    }

}

因此,我想做的是更改labelQuestion文本,以便在用户出错时提供正确的答案。

我尝试了以下几个选项:

labelQuestion.text = "The correct answer was: \(currentCorrectAnswerIndex)"

labelQuestion.text = "The correct answer was: \(currentCorrectAnswerIndex)" as string

let answertext = String(currentCorrectAnswerIndex)

labelQuestion.text = "The correct answer was: \(answertext)"

所有这些尝试都有相同的结果 - 它们提供与具有正确答案的按钮对应的整数值。例如,向用户显示如下消息:

正确的答案是:2 而不是 正确的答案是:星球

我知道我必须犯一个新手的错误,但任何方向都会非常感激。我正在使用最新版本的Xcode / Swift。

0 个答案:

没有答案