2015-10-21 55 views
-2

我對此很陌生,經過大量的「未解析的標識符」搜索後,我似乎沒有得到此錯誤消失。在「viewDidLoad()」的函數調用中出現「未解析的標識符」錯誤,其中顯示「startGame()」。在Swift函數調用中的未解析標識符

我在本教程中立足這一點:http://www.raywenderlich.com/114262/learn-to-code-ios-apps-with-swift-tutorial-4-your-first-app

功能應該啓動定時器應用程序啓動時。

override func viewDidLoad() { 
    super.viewDidLoad() 
      startGame() 
    //SecondTimerValue.delegate = self 
    SecondTimerValue.keyboardType = UIKeyboardType.NumberPad 
    // Do any additional setup after loading the view, typically from a nib. 
} 
func startGame() { 
     seconds = 30 
     count = 0 
     FirstTimerLabel.text = "Time: \(seconds)" 
     scoreLabel.text = "Score: \(count)" 

     timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true) 
    } 

這是整個代碼。

import UIKit 

class ViewController: UIViewController { 


@IBOutlet var scoreLabel: UILabel! 
@IBOutlet var FirstTimerLabel: UILabel! 
@IBOutlet var SecondTimerValue: UITextField! 
var count = 0 
var seconds = 0 
var timer = NSTimer() 
var SecondTimerValueVar = 0 

// Goal 1 - verify input in text field went to variable 
// Goal 2 - get rid of keyboard after "enter" 
// Goal 3 - use new value to start new timer 
// Goal 4 - When 1st timer reaches zero, start second timer 



override func viewDidLoad() { 
    super.viewDidLoad() 
      startGame() 
    //SecondTimerValue.delegate = self 
    SecondTimerValue.keyboardType = UIKeyboardType.NumberPad 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


@IBAction func buttonPressed(){ 
    SecondTimerValueVar = SecondTimerValue.text.toInt()! 
    scoreLabel.text = "Score \(count)" 
    count++ 
} 

@IBAction func startTimerPressed(){ 
    //let SecondTimerValueVar = Int(SecondTimerValue) 

} 

@IBAction func userTappedBackground(sender: AnyObject) { 
    view.endEditing(true) 


//let a:Int? = Int(firstText.text)  // firstText is //UITextField 
//let b:Int? = Int(secondText.text) // secondText is UITextField 

func startGame() { 
     seconds = 30 
     count = 0 
     FirstTimerLabel.text = "Time: \(seconds)" 
     scoreLabel.text = "Score: \(count)" 

     timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true) 
    } 


func subtractTime(){ 
    seconds-- 
    FirstTimerLabel.text = "Time: \(seconds)" 

    if (seconds==0) { 
     timer.invalidate() 
    } 
} 
} 
+0

不要忘記@馬特的回答是正確的,因爲它解決了你的問題! – Buggy

回答

1

的問題是在這裏:

@IBAction func userTappedBackground(sender: AnyObject) { 
    view.endEditing(true) 
func startGame() { 

你忽略了右大括號爲userTappedBackground,所以startGame裏面,不能被看作是一種方法。

+0

已解決!謝謝! – Jon

+2

@Jon正確的縮進將幫助您更輕鬆地找出問題。如果你看到這樣一個奇怪的錯誤,讓Xcode重新使用control-i來重新生成文件中的所有代碼。 – Gavin

+0

@Gavin不錯的提示 – matt

相關問題