实现 UIStepper 时对 "value" 的使用不明确

Ambiguous use of "value" while implementing UIStepper

我正在尝试实现一个将其值反映到标签上的简单步进器。 使用 "Int(sender.value)" 时出现错误 "Ambiguous use of value"

//  ViewController.swift
//  Stepper
//
//  Created by Prabhu Konchada on 19/06/15.
//  Copyright (c) 2015 Prabhu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var StepperValue: UILabel!

    @IBOutlet weak var OutputLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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 StepperTap(sender: AnyObject) {

        self.OutputLabel.text = String(Int(sender.value).description)
    }
}

尝试像这样修改您的代码:

@IBAction func StepperTap(sender: UIStepper) {

    self.OutputLabel.text = "\(sender.value)"
}

您可以按如下方式操作:

@IBOutlet var lblStep: UILabel!
@IBAction func stepPressed(sender: UIStepper) {

    lblStep.text = sender.value.description

}

否则你必须将 AnyObject 转换为 UIStepper

var stepControl : UIStepper = sender as! UIStepper

像 :

@IBAction func StepperTap(sender: AnyObject) {
    var stepControl : UIStepper = sender as! UIStepper
    self.OutputLabel.text = stepControl.value.description
}

6.1 版的全部代码你只需要改成 as with as 即可!对于 6.3.2:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var lblStep: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // 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 stepPressed(sender: AnyObject) {

        var step : UIStepper = sender as UIStepper

        lblStep.text = step.value.description

    }
}