Expression type '@lvalue String?' is ambiguous without more context

前端 未结 2 2126
Happy的楠姐
Happy的楠姐 2021-01-28 13:43

Out of nothing I got this error Expression type \'@lvalue String?\' is ambiguous without more context in my code:

    if textView.text != \"\" &         


        
2条回答
  •  情书的邮戳
    2021-01-28 14:27

    Two things are wrong:

    textView.text != "" You are comparing an optional String with a non-optional one.

    Replace by:

    if let text = textView.text, text != ""

    Then in your constructor, don't unwrap the text!

    If you want keep the optional :

    self.text = text ?? "" //will avoid crash

    Or better:

    init(text: String = "")

提交回复
热议问题