我對此很陌生,經過大量的「未解析的標識符」搜索後,我似乎沒有得到此錯誤消失。在「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()
}
}
}
不要忘記@馬特的回答是正確的,因爲它解決了你的問題! – Buggy