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

前端 未结 2 2097
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:07

    In the code above,

    1. Use userLabel?.text directly without unwrapping anything. addedByUser is of type String?.

    2. You're using UserImage type for userImage parameter. Use a UIImage instance or nil instead.

    3. You haven't added data parameter in your code.

    Here is the code,

    let newJob = Job(text: textView.text, jobImage: takenImage, addedByUser: userLabel?.text, userImage: nil, location: userLocation.text, passMap: takenLocation, userID: userID, postID: key, data: [:])
    

    Avoid unnecessarily force-unwrap the optionals. It might result in runtime exception.

    0 讨论(0)
  • 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 = "")

    0 讨论(0)
提交回复
热议问题