Can't change input Value in Swift 4.2

269 Views Asked by At

I have an input UITextField and want to change his value.

I defined this UITextField like this:

@IBOutlet weak var valueInput: UITextField!

Than in viewDidLoad() I make delegate - self:

valueInput?.delegate = self

and then in viewDidLoad I try to assign value to input like this:

if let priceIn = self.valueIn Input {
    valueIn.text! = strPr // (this is var and it has String format I've checked it, and I also try to make print(strPr) and I get a value)
}

Unfortunately this string is not working:

valueIn.text! = strPr

But if I write something like this

valueIn.text! = "11"

This one will work.

I've tried to to something like this also, but it's not working also:

valueIn.text! = "\(strPr)"

Anyone have any ideas?

Thanks for any answer, hope somebody had the same problem and know how to resolve it.

1

There are 1 best solutions below

2
On

These lines are wrong:

valueIn.text! = strPr
valueIn.text! = "11"

valueIn.text! should only appear on the right side, in case you are sure valueIn references a valid object (not nil) and text is declared as var text: String?

Not sure exactly how your variables are declared, but try something like this:

valueIn.text = "11"

Or, if strPtr is an Optional, try this:

if let strPr = strPr {
  valueIn.text = strPr
  print("New valueIn.text: \(valueIn.text!)")
}