0

How can I assign a UILabel in my app with an NSString expression? My attempt was:

@IBOutlet weak var imput: UILabel!
@IBOutlet weak var output: UILabel!

...

...

...

@IBAction func equal(sender: AnyObject) {
        calculate = NSExpression(format: imput.text!)
        output = calculate.expressionValueWithObject(nil, context: nil) as! UILabel
    }
Kocsis Kristof
  • 74
  • 2
  • 10
  • 1
    Do you *really* want to create / set a UILabel? Do you not maybe just want to assign the result of the evaluation to the labels text property to display it? – luk2302 May 28 '16 at 16:58
  • First case: have you defined a custom function returning a label? Second case: do it! – luk2302 May 28 '16 at 16:59
  • No I haven't created a function for this, but how would I do it? I have never worked with NSExpression before. – Kocsis Kristof May 28 '16 at 17:21
  • ***You*** have to tell us what you want to exactly and what you have tried to achieve that! It actually sounds like you want to do the second case. For defining functions and the first case you have to do some googling. – luk2302 May 28 '16 at 17:29
  • I am making a calculator and there is an imput, I can enter a formula something like "3+3*5" or "sqrt(9)+1" and when I press "=" I take imput.text as a string, calculate it and write it out it in another label called output as a string. I have tried what I have posted. I don't know how to create a function for such purpose. – Kocsis Kristof May 28 '16 at 17:39
  • I was trying to post an answer but the playground is not working right now :/ You have to assign the result of the `expressionValueWithObject` to a double or something similar. And then if the double actually contains anything convert the double to a string, e.g. via `"\(yourDoubleValue)"` to `output.text`. – luk2302 May 28 '16 at 18:04
  • So I found the core problem: I wrote "output =" instead of "output.text". And instead of "as! UILabel" I converted it using another variable. – Kocsis Kristof May 29 '16 at 11:10

1 Answers1

0

I had to use output.text = ... instead of output = ... and instead of as! UILabel I used two other variables to convert the NSExpression to Double and than to String.

Kocsis Kristof
  • 74
  • 2
  • 10