2016-09-16 40 views
-2

幾天前我剛剛開始學習Swift,並且在遵循教程時遇到了這個問題。 I tried to add the code in text but was having formatting problems so I had to go with an image of the code:(Swift dilema(標題很難想出)

任何幫助,將不勝感激。

這是更正後的代碼。 進口的UIKit

class ViewController: UIViewController { 
var currentValue: Int = 0 
var targetValue: Int = 0 
func startNewRound() { 
    targetValue = 1 + Int(arc4random_uniform(100)) 
    currentValue = 50 
    slider.value = Float(currentValue) } 

func updateLabels() { 
     targetLabel.text = "\(targetValue)" 
    } 


@IBOutlet weak var slider: UISlider! 
@IBOutlet weak var targetLabel: UILabel! 


override func viewDidLoad() { 
    super.viewDidLoad() 
    startNewRound() 
    updateLabels() 

    // 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 showAlert() { 
    let alert = UIAlertController(title: "Hello World", 
     message: "The value of the slider is \(currentValue)" 
     + "\nThe target value is: \(targetValue)", 

     preferredStyle: .alert) 
    let action = UIAlertAction(title: "Ok", style: .default, 
           handler: nil) 
    alert.addAction(action) 

    present(alert, animated:true , completion: nil) 
    startNewRound() 
    updateLabels() 
} 
@IBAction func sliderMOved(slider: UISlider) { 
    currentValue = lroundf(slider.value) } 

} 
+3

你的「startNewRound」方法沒有關閉'}',下面的「updateLabels」在「startNewRound」裏面。注意你如何縮進你的代碼,這不會發生。 – Moritz

+0

編輯你的問題並添加代碼,如果有任何你不知道如何修復的格式問題,其他人可以編輯你的問題來幫助你。順便說一句,歡迎來到StackOverflow。 –

+0

謝謝!這是一個支架問題 – Retsnad

回答

2

。在你的ViewController類沒有方法updateLabels()。請看:

class ViewController: UIViewController { 
    func startNewRound() { 
     targetValue = 1 
    func updateLabels() { 
     targetLabel.text = "\(targetValue)" } // <- see? 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     updateLabels() 
    } 
} 

你可能沒看出來,但看看它的樣子,當我們解決您的代碼的格式:

class ViewController: UIViewController { 
    func startNewRound() { 
     targetValue = 1 
     func updateLabels() { 
      targetLabel.text = "\(targetValue)" 
     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     updateLabels() 
    } 
} 

你現在看到了嗎?在startNewRound()方法中有一個名爲updateLabels()的本地函數,但編譯器有抱怨說在那裏存在未解析的標識符的權利。

+0

感謝您的幫助! – Retsnad

+0

無論如何,將代碼添加到您的問題。這個問題在未來幾年仍將在StackOverflow上保留。如果有人想要閱讀它,這是很好的禮儀。 –

+0

你是對的,但我只想教新人一些禮節;) –

1

我認爲你有一個錯字

func startNewRound() { 
    // Some stuff 
    func updateLabels() { 
     // Some stuff 
} 

你應該有

func startNewRounds() { 
    // Some stuff 
} 
func updateLabels() { 
    // Some Stuff 
} 
+0

我知道。我只是告訴你,你在'startNewRound'裏面寫了'updateLabels'函數。這就是爲什麼你有錯誤'使用未解決的標識符'。 – Zico