rahmikg
rahmikg

Reputation: 75

Swift 3 Convert Double to String

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
}

Upvotes: 7

Views: 13433

Answers (1)

Abhra Dasgupta
Abhra Dasgupta

Reputation: 525

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)"
}

Upvotes: 8

Related Questions