2017-04-02 78 views
-1

我不確定爲什麼我的標籤不會更改爲問題字符串。如何在Swift中調用函數?

func GenerateQuestion(){ 
    let FirstNo = Int(arc4random_uniform(10))+1 // first number in sum 
    let SecondNo = Int(arc4random_uniform(10))+1 // second number in sum 
    let SymbolNo = Int(arc4random_uniform(4))+1 // number which decides the operator in sum 
    var OperatorSymbol = "" 
    var Question = "" // the question 
    var Answer = Int() // holds answer of the sum 
    var ButtonNo = Int(arc4random_uniform(4))+1 // number which chooses button to hold correct answer 

    switch SymbolNo{ 
    case 1 : OperatorSymbol = "+"; 
    Question = "\(FirstNo) + \(SecondNo) = ?"; 
    self.Lbl.text = Question 
    case 2 : OperatorSymbol = "-"; 
     Question = "\(FirstNo) - \(SecondNo) = ?"; 
     self.Lbl.text = Question 
    case 3 : OperatorSymbol = "/"; 
     Question = "\(FirstNo)/\(SecondNo) = ?"; 
     self.Lbl.text = Question 
    case 4 : OperatorSymbol = "x"; 
     Question = "\(FirstNo) x \(SecondNo) = ?;"; 
     self.Lbl.text = Question 
    default : Question = "" 
    } 

我沒有叫func generateQuestion,因爲我不知道怎麼樣。我該怎麼辦?

+1

正如官方設計指南所述,通常建議在Swift中使用駝峯函數名稱:http://swift.org/documentation/api-design-guidelines – the4kman

+0

您聲明 - *「我不確定爲什麼我的標籤不會改變「*。然後你聲明 - *「我還沒有調用'func generateQuestion'」*。你只是回答你自己的問題。 – rmaddy

+3

*「我該怎麼辦?」 - 閱讀[Swift編程語言](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/)書。 – rmaddy

回答

1

要調用函數的實例,請輸入函數名稱,後跟一對帶括號或不帶參數的括號。

generateQuestion() 

如果您希望在應用程序加載後立即執行某些操作,則應將其輸入到viewDidLoad函數中。如上所述

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    generateQuestion() 
} 

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

這是一個很好的資源。

同樣,如上所述,最好使用lowerCamelCase作爲函數和變量,併爲類和結構保留UpperCamelCase。更多可以在Swift指南中找到。

祝你好運!

+0

大幫幫凱爾,謝謝 – dstaton

相關問題