2

I am trying to implement a simple stepper which reflects its value onto a label. While using "Int(sender.value)" I am getting an error "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)
    }
}
Prabhu Konchada
  • 548
  • 8
  • 26
  • why you are converting the value to Int ? – Ashish Kakkad Jun 20 '15 at 05:05
  • @AshishKakkad I tired without it which successfully builds but throws this error 0x1947e2a08) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) . So I followed this tutorial http://www.ioscreator.com/tutorials/uistepper-tutorial-ios8-swift – Prabhu Konchada Jun 20 '15 at 05:09
  • @AshishKakkad I think the problem was using AnyObject instead of sender :) – Prabhu Konchada Jun 20 '15 at 05:50

2 Answers2

3

You can do like as follows :

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

    lblStep.text = sender.value.description

}

else you have to convert AnyObject to UIStepper like as

var stepControl : UIStepper = sender as! UIStepper

as like :

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

Whole code as version 6.1 you have to just change to as with as! for 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

    }
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • 2015-06-20 10:57:00.261 Stepper[19697:2335171] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Action.' *** First throw call stack: (0x1829902d8 0x1941640e4 0x18298ff5c 0x1837c9480 0x1828bb520 0x1877f07a0 0x1876ec384 0x1873d0a28 0x1873d0994 0x1873d71d0 0x1873d4880 0x180d9cd24 0x1874468ec 0x18765aa94 0x18765d208 0x18765b778 0x18b1a13c8 0x18294827c ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) – Prabhu Konchada Jun 20 '15 at 05:27
  • The Second : AnyObject cannot be converted to UIStepper – Prabhu Konchada Jun 20 '15 at 05:29
  • I too have checked it can you upload the file ?? – Prabhu Konchada Jun 20 '15 at 05:30
  • Version 6.3.2 (6D2105) – Prabhu Konchada Jun 20 '15 at 05:34
  • Tried it Didn't work : 2015-06-20 11:05:14.295 Stepper[19725:2337567] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: – Prabhu Konchada Jun 20 '15 at 05:36
  • @Ashsih Kakkad Probably something wrong with my project file worked on another one I am deleting this question Sorry My Bad :) – Prabhu Konchada Jun 20 '15 at 05:46
1

Try to modify your code like this:

@IBAction func StepperTap(sender: UIStepper) {

    self.OutputLabel.text = "\(sender.value)"
}
MirekE
  • 11,515
  • 5
  • 35
  • 28
  • It throws an error saying : Could not find member 'init' ; Anyway may I Know what does '\' do ?? – Prabhu Konchada Jun 20 '15 at 05:23
  • 1
    The main issue here is the type of the `sender` variable. When you created the IBAction, you did not select UIStepper, but AnyObject. The \\(sender.value) is called string interpolation - it inserts value of sender.value into a string. using sender.value.description will do the same thing. – MirekE Jun 20 '15 at 05:31
  • Thank you For your time MirekE – Prabhu Konchada Jun 20 '15 at 05:52