問答題app-初階版
Aug 28, 2017 · 4 min read
先寫一個不用struct或class的,這樣才可以體會struct或class的好!不用class或struct,一個題目或答案就要配一個常數,10題就20個常數了,會增加寫程式許多麻煩。
let quesiotn1 = "湖人KO連線是那兩個人"let question2 = "灰熊雙塔是那兩個人"let question3 = "馬刺雙塔是那兩個人"let answer1 = "Kobe O'Niel"let answer2 = "gasol Randolf"let answer3 = "Duncan Robison"var Number = [1,2,3]let chooseNumber = GKShuffledDistribution(lowestValue: 0, highestValue: 2)var questionAmount = 0func choosing(){let trueNumber = chooseNumber.nextInt()let questionNumber = Number[trueNumber]print(questionNumber)if questionNumber == 1 {questionLabel.text = quesiotn1}if questionNumber == 2 {questionLabel.text = question2}if questionNumber == 3 {questionLabel.text = question3}}@IBAction func theAnswer(_ sender: Any) {questionAmount = questionAmount + 1if questionLabel.text == quesiotn1{answerLabel.text = answer1}if questionLabel.text == question2 {answerLabel.text = answer2}if questionLabel.text == question3{answerLabel.text = answer3}if questionAmount == 3 {responseAnswerButton.isEnabled = falsenextQuestionButton.isEnabled = false}}@IBAction func restsrAction(_ sender: Any) {responseAnswerButton.isEnabled=truenextQuestionButton.isEnabled = true}@IBAction func nextButtonAction(_ sender: Any) {choosing()answerLabel.text = ""}
用struct來寫,如下:

struct跟class的差別