Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Convert Double to String

Tags:

swift3

core

I keep getting this error "Initializer for conditional binding must have Optional type, not 'Double'". I am trying to display some core data values and this one is a double. Ive tried to work around it the same way I had to do to store the values when converting it.

heres the code that gives me the error:

func displayStats() {



        // display other attributes if they have values
        if let servingSize  = mealstats.serving {
            servingsLabel.text = servingSize

        }
like image 711
rahmikg Avatar asked Nov 05 '16 20:11

rahmikg


1 Answers

mealstats.serving is most probably of type "Double" and not "Double?"
Since it is not optional it cannot be unwrapped. The right way to use it would be

func displayStats() {
    // display other attributes if they have values
    servingsLabel.text = "\(mealstats.serving)"
}
like image 153
Abhra Dasgupta Avatar answered Sep 24 '22 18:09

Abhra Dasgupta