can not convert value type "string?" to expected argument type "inout string"

24,349

Solution 1

You need to write it like this instead:

self.displayResultLable.text =  self.displayResultLable.text! + title as! String

It´s because of the left side is an optional and the right side is not and they don´t match. That´s why you need to write label.text = label.text +...

I can also suggest you to change your if let to this instead:

if let title = books.valueForKey("title") as? String {
   self.displayResultLable.text = (self.displayResultLable.text ?? "") + title
}

Solution 2

I suggest you use the optional chaining operator to only perform the addition of text if the optional (self.displayResultLable.text) is non-nil:

self.displayResultLable.text? +=  (title as! String)
Share:
24,349
Admin
Author by

Admin

Updated on July 15, 2022

Comments

  • Admin
    Admin almost 2 years

    this line self.displayResultLable.text += (title as! String) error throwing

    can not convert value type "string?" to expected argument type "inout string"

    Here is my code :

       if results.count > 0 {
                        var displayResult : String?
                        for books in results as! [NSManagedObject] {
    
                            if let title = books.valueForKey("title") {
    
                                self.displayResultLable.text +=  (title as! String)
    
                            }
                        }
                    }
    

    what is the inout string here ? what is the best practice ?

    Note this line self.displayResultLable.text = (title as! String) working fine:

  • Nazmul Hasan
    Nazmul Hasan over 7 years
    displayResultLable.text is option type ?
  • Rashwan L
    Rashwan L over 7 years
    @twilight2, it´s because of the left side is an optional and the right side is not and they don´t match. That´s why you need to write label.text = label.text +...
  • dfrib
    dfrib over 7 years
    What if self.displayResultLable.text has not yet been assigned a value (i.e., has value nil)? Then the explicit unwrapping of it in ... = self.displayResultLable.text! ... will yield a runtime exception. Using the explicit ("forced") unwrapping should never be encouraged, not unless it has been asserted that the object for unwrapping is non-nil. A more safe approach would be to e.g. use the nil coalescing operator to set a defailt "current state" of the text in case its String property is nil. E.g. self.displayResultLable.text = (self.displayResultLable.text ?? "") + title
  • dfrib
    dfrib over 7 years
    Also, note that there exist no overload of the += for optionals (i.e., no += operator even if both lhs and rhs are optionals of the same wrapped type). The reason is quite apparent; given two optionals (of same wrapped type), say lhs and rhs, if either lhs or rhs is nil, what would be the resulting value of lhs for lhs += rhs?
  • Admin
    Admin over 7 years
    @dfri your comment really useful . thank you so much
  • dfrib
    dfrib over 7 years
    @twilight2 happy to help.