0

Here is the code, I'm trying to make a simple calculator :

@IBAction func btnOperatorInput(sender: UIButton) {
    let op = ("+", "-", "*", "/")

    let nr1 = sender.currentTitle!
    var nr2 = ""
    if sender.currentTitle == nr1{

        display.text! = self.display.text! + nr1
        nrTaped = true

    } else if sender.currentTitle == String(op) {
        nrTaped = false

        nr2 = display.text!
        display.text! = String(String(nr1) + String(op))

    }

    for _ in op.0{

     self.calc.add(Int(nr1)!, secondNumber: Int(nr2)!)

    }



}

I get the error at the for loop. I don't know how to use the "+" (for example) from the String I created, when I press the + to add the numbers together!

Hamish
  • 78,605
  • 19
  • 187
  • 280
Dani Turc
  • 39
  • 2
  • 11
  • 2
    I think the error says it best... you're can't iterate over a string, as it isn't a sequence. What exactly are you trying to do there? Also you should stop force unwrapping everything. Please learn how to [properly deal with optionals](http://stackoverflow.com/a/36360605/2976878). – Hamish Apr 07 '16 at 14:59
  • I wanna use the "+" from the string, pass it to the button + and after, make the + opperation... – Dani Turc Apr 07 '16 at 15:01
  • 2
    I'm confused as to why you are using a for loop there. – Slayter Apr 07 '16 at 15:02
  • Swift Strings do not conform to SequenceType anymore, I believe since version 2. – ff10 Aug 05 '16 at 11:43

1 Answers1

1

The error is easily solved, you need to access the String's CharacterView:

for _ in op.0.characters {

       self.calc.add(Int(nr1)!, secondNumber: Int(nr2)!)

}

Note: As another commenter has queried above, I'm not sure why you'd want to iterate over what will always be a single and predictable character but I guess this might just be example code.

sketchyTech
  • 5,746
  • 1
  • 33
  • 56