Issue converting string to double

not sure how to fix error message from incorrectly converting string from text field input to double to be used in other part of code

also may have unwrapped optional wrong, not even sure my syntax is right when declaring it as an optional

thanks for your help!!!

import SwiftUI

struct ContentView: View {
//    @State private var xRadius = 150   // default values for optional
//    @State private var yRadius = 75   // default values for optional
//    
//    @State private var: Double(xRadius2) = 0
//    @State private var: Double(yRadius2) = 0
    
    @State private var xRadiusInput: String? = " "
    @State private var yRadiusInput: String? = " "
    
        var xRadius: CGFloat
        {
            CGFloat(Double?(xRadiusInput) ?? 150)
        }
        
        var yRadius: CGFloat
        {
            CGFloat(Double?(yRadiusInput) ?? 75)
        }
   
    
    var speed: CGFloat = 0.5 
 
    var body: some View {
        TimelineView(.animation) { timeline in
            var speed = (xRadius + yRadius) / 100
            let time = timeline.date.timeIntervalSinceReferenceDate
            let angle = time * speed * 2 * .pi   
                    
            let x = cos(angle) * xRadius //angle is where you are on the circle, cos and sin are the x                           
            let y = sin(angle) * yRadius
                                            

            
            Text("X dimension: , \(xRadius)")
            TextField("Enter x dimension of elliptical orbit", text: $xRadiusInput)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            Text("Y dimension:  \(yRadius)")
            TextField("Enter y dimension of elliptical orbit", text: $yRadiusInput)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
            
            ZStack {
                Ellipse()
                    .stroke(Color.gray.opacity(0.3), lineWidth: 2)
                    .frame(width: xRadius * 2, height: yRadius * 2)
                    // .offset(x: 50, y: 3)

                Circle()
                    .fill(Color.blue)
                    .frame(width: 30, height: 30)
                    .offset(x: x, y: y)  // x / y as in lines 22 and 23
                    
                
                Circle()
                    .fill(Color.red)
                    .frame(width: 75, height: 75)
            }
            .frame(width: xRadius * 2 + 60, height: yRadius * 2 + 60)
        }
    }
}

#Preview {
    ContentView()
}


The problem actually starts with making your @State a String? from what I can tell.

Hold on... I was wrong about the previous fix...

This is a full fix:

At the top

    @State private var xRadiusInput: String = " "
    @State private var yRadiusInput: String = " "
    
    @State private var xRadius: CGFloat = 150
    @State private var yRadius: CGFloat = 75
    
    func updateCGFloat(_ string:some StringProtocol, fallBack:Double) -> CGFloat {
        
        CGFloat(Double(string) ?? fallBack)
    }

at the bottom:

            ZStack {
                Ellipse()
                    .stroke(Color.gray.opacity(0.3), lineWidth: 2)
                    .frame(width: xRadius * 2, height: yRadius * 2)
                    // .offset(x: 50, y: 3)

                Circle()
                    .fill(Color.blue)
                    .frame(width: 30, height: 30)
                    .offset(x: x, y: y)  // x / y as in lines 22 and 23
                    
                
                Circle()
                    .fill(Color.red)
                    .frame(width: 75, height: 75)
            }
            .frame(width: xRadius * 2 + 60, height: yRadius * 2 + 60)

        }.onChange(of:xRadiusInput) {
            xRadius = updateCGFloat(xRadiusInput, fallBack: 150)
        }
        .onChange(of:yRadiusInput) {
            yRadius = updateCGFloat(yRadiusInput, fallBack: 75)
        }

Does this make sense? Would you like more of an explanation?

There are some fancier ways making a reusable custom field input widget, but that is more a SwiftUI problem than really a String to Double problem!

im also getting "Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding' " now at the text field lines of $xRadiusInput

yes I would like some explanation

Exactly.

So you're not really having "converting string to double" problems, you're having working with @State and @Binding in SwiftUI problems.

  • @State is used when you have a variable that when it changes, the view should also change. Changing variables that aren't decorated with @State will not update the view.
  • @Binding is one way to get information between views. The child view in this case (a TextField) wants a Binding<String>.

I'm going to point you at the hackingwithswift.com website which has great a forum for SwiftUI.

In fact while looking through it I found a solution that is even better than the one I gave

I don't "work here" but my understanding that since SwiftUI isn't managed by the Swift foundation you'll get slower answers here than there.

1 Like