Invalid frame dimension (negative or non-finite) when trying to define Capsule frame height in swiftUI

Viewed 871

I'm trying to define the height of a capsule in SwiftUI from a structure da that transforms String into double and does some calculation over it, just like the example bellow.

struct graphLenght{
       var energiaOne: String
       
       init(energiaOne: String){
           self.energiaOne = energiaOne
           
       }
       
       func energyOne() -> Double{
           let formatter = NumberFormatter()
           formatter.locale = Locale(identifier: "pt_BR")
           formatter.numberStyle = .decimal
           let energyOne = formatter.number(from: energiaOne) ?? 00

           return Double(truncating: energyOne)
       } 

       func maxValue() -> Double{
           return max(energyOne(), energyTwo(), energyThree())
       }
       
       func maxValueGraph() -> Double{
           if maxValue() <= 0 {
               return 1
           }else{
           return round(maxValue() * 1.1)
           }
       }
       
       func lenghtConcrete() -> CGFloat{
           return CGFloat(((225 * energyOne())/maxValue()))
       }

although when I try to use graph.lenghtConcrete in .frame (just like bellow) the warning invalid Frame dimension (negative or non-finite) keeps showing.

Capsule()
    .frame(width: 14, height: abs(CGFloat(graph.lenghtConcrete())))
    .foregroundColor(.blue)

One observation the return of maxValueGraph() is always 1

How can I solve this issue??

0 Answers
Related