迅捷的困境(标题难以想象)

时间:2016-09-16 11:35:58

标签: swift

我刚开始学习Swift几天前,我在学习教程时遇到了这个问题。  http://saxon.sourceforge.net/saxon6.5.5/extensibility.html

任何帮助都将不胜感激。

这是更正后的代码。     导入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) }

}

2 个答案:

答案 0 :(得分:2)

updateLabels()课程中没有方法ViewController。看看:

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()
    }
}

你现在看到了吗?在updateLabels()方法中有一个名为startNewRound()的本地函数,但编译器抱怨那里有一个未解析的标识符是正确的。

答案 1 :(得分:1)

我认为你有一个错字

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

你应该

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