0

Every time a stepper is pressed, how do you add 1 or subtract 1 from an Int.

This is the action of the stepper.

@IBAction func stepper(sender: AnyObject) {
       self.numberLabel.text = "\(Int(stepper.value))"
  totalTextField.text += "1"
} 

There is a error totalTextField.text. The error is "string" is not identical to 'CGFloat'.

Thanks

Dawson Seibold
  • 145
  • 2
  • 11

1 Answers1

2

You first need to convert the number to an integer value, like this:

totalTextField.text = "\(totalTextField.text.toInt() + 1)"

The reason for this is that 1 and "1" are not the same thing, as they vary in type. 1 is an integer, while "1" is a string. To make it a little clearer, usually "1" + "1" = "11", in most programming languages, while 1 + 1 = 2, obviously.

Dawson Seibold
  • 145
  • 2
  • 11
André Fratelli
  • 5,920
  • 7
  • 46
  • 87
  • I'm not sure what you are asking now, you want to subtract instead of adding? – André Fratelli Oct 12 '14 at 04:18
  • When I Click the + button on the stepper and then when I click the - button I want to subtract. sorry I know it is confussing – Dawson Seibold Oct 12 '14 at 04:22
  • I'm not sure what you mean by + and - button, is that on the keyboard or you have actual buttons on your UI? – André Fratelli Oct 12 '14 at 04:36
  • Do you know what a stepper is it is the box around a - and a + sign. – Dawson Seibold Oct 12 '14 at 04:40
  • I added a picture of a stepper – Dawson Seibold Oct 12 '14 at 04:42
  • Sorry, I missed that. You do know that steppers already make this count, right? – André Fratelli Oct 12 '14 at 04:46
  • Take a look: https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIStepper_Class/index.html#//apple_ref/occ/instp/UIStepper/value – André Fratelli Oct 12 '14 at 04:47
  • Also see this: http://stackoverflow.com/questions/17433679/uistepper-how-to-be-aware-which-button-minus-or-plus-button-of-the-stepper-ha – André Fratelli Oct 12 '14 at 04:48
  • sorry i am new to coding and i really didnt understand that.I know i am asking alot from you but if you could kina explain it? – Dawson Seibold Oct 12 '14 at 05:30
  • Instead of `AnyObject` try using `UIStepper` instead. Then read the value from `sender.value`, which should contain the value already incremented/decremented. As such you don't need to change the value yourself, because `UIStepper` is already doing this for you. If that works, please mark the answer as accepted. – André Fratelli Oct 12 '14 at 12:36
  • So what tells it to add or subtract to the TotalBoxesTextField? – Dawson Seibold Oct 12 '14 at 15:03
  • You don't need to add to it, you just want to display the value that `UIStepper` already has. Therefore, something like `TotalBoxesTextField.text = sender.value` should suffice, given that `sender` is of type `UIStepper` – André Fratelli Oct 12 '14 at 15:27
  • I don't have xcode 6 here to test for this, so I'm not testing my suggestions. I do believe it should be `"\(sender.value)"` instead, and perhaps the `sender` should not be `AnyObject`, but `UIStepper` instead – André Fratelli Oct 12 '14 at 16:42
  • Ok thanks that worked but this would just make it look better. it comes out as 1.0, 2.0 and so on and so forth but could you tell me how to get it to take of the ".0" part. – Dawson Seibold Oct 12 '14 at 18:37
  • Ok this might make it easer to understand what i need. So I added a picture of what it looks like. so the code you gave me works but i need it to add to the sum already in the TextField. – Dawson Seibold Oct 12 '14 at 19:03
  • You have many alternatives to accomplish that. Try casting to integer, rounding, or string formatting (like '%d'). However, your problem does not relate at all with your initial question and that as been answered many times on stackoverflow and other communities. My opinion is that you should mark the answer as accepted and search for solutions for your other problems or open new issues. – André Fratelli Oct 17 '14 at 16:53