Matthew
Matthew

Reputation: 159

Struggling to turn a Double into a String to put inside a text from an object array SWIFTUI (SWIFT)

I am trying to put the following double value from my environment object into a text fo the user to see. He is a small part of the code where I am getting an error, saying the value of type String must be unwrapped:

struct myView:View{
    @EnvironmentObject var getFood:FoodAddModel

    var unwrappedFoods:[AddedFoods]{
        getFood.foods ?? []
    }
    
    var body: some View{
        NavigationView{
        List{
        ForEach(unwrappedFoods) {obj in
            let this: String? = String(obj.totalCals)
            Text(obj.name)
            Text(this)
                }
        }
    }
    }
}

The full file code:

import Foundation


import SwiftUI

struct AddedFoods:Identifiable{
    var name: String = ""
    var totalCals: Double = 0
    var totalProtein: Double = 0
    var totalCarbs: Double = 0
    var totalFat: Double = 0
    var id = UUID().uuidString
   //Your other properties
}


class FoodAddModel: ObservableObject,Identifiable {
    
    @Published var foods : [AddedFoods]?
    
    var id = UUID().uuidString

    init() {
        dummyData()
    }
    
    func dummyData() {
        var obj:[AddedFoods] = []
        obj.append(AddedFoods(name: "Pasta", totalCals: 340, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Chicken", totalCals: 560, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Apple", totalCals: 54, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Noodles", totalCals: 231, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        foods = obj
    }
}

struct myView:View{
    @EnvironmentObject var getFood:FoodAddModel

    var unwrappedFoods:[AddedFoods]{
        getFood.foods ?? []
    }
    
    var body: some View{
        NavigationView{
        List{
        ForEach(unwrappedFoods) {obj in
            let this: String? = String(obj.totalCals)
            Text(obj.name)
            Text(this)
                }
        }
    }
    }
}

Any help would be greatly appreciated :)

Upvotes: 1

Views: 168

Answers (1)

Andrew
Andrew

Reputation: 28539

You can just use String interpolation to show the Double value in the Text. You can read more about String interpolation here, and if you need a specific number of decimal places you can do that too using String(format:) which you can read more about here.

Something like this should work.

struct myView:View{
    @EnvironmentObject var getFood:FoodAddModel

    var unwrappedFoods:[AddedFoods]{
        getFood.foods ?? []
    }

    var body: some View{
        NavigationView{
            List{
                ForEach(unwrappedFoods) {obj in
                    Text(obj.name)
                    Text("\(obj.totalCals)")
                    // Or if you need a fixed number of decimal places
                    Text(String(format: "%.0f", obj.totalCals))
                    Text(String(format: "%.2f", obj.totalCals))
                }
            }
        }
    }
}

Note, by convention structs should begin with a capital letter so your struct myView should really be named MyView.

Upvotes: 2

Related Questions