How to fix this TextField to variable error?

I want to import text from TextField(SwiftUI) in float format but I just have this error, the message said “Can’t turn the number of type ‘float’ to a unexpected type ‘Binding’”, if I try to run it will also send me the same error, how do I fix it?

  • I expect Apple will answer this question in Chinese.

Add a Comment

Accepted Reply

You need to change to

$Calculated

and

$Calculator

But Float is not a text. So you should call

TextField("Calculated number", value: $Calculated, format: .number)

and

TextField("Calculator", value: $Calculator, format: .number)

But doing so, labels will not show.

This works better:

HStack {Text("Calculated number"); TextField("", value: $calculated, format: .number) }

and

HStack {Text("Calculator"); TextField("", value: $calculator, format: .number) }

Note also that var names should start with lowercase: calculated and calculator.

  • Other error: Cannot find ‘$calculated’ and ‘$calculator’

  • Normal, I changed to have first letter as lowercase (calculator vs Calculator, calculated vs Calculated). So, you need to change it everywhere in code.

  • I changed the first letter to lowercase, but it still show ‘didn’t find $calculator in scope’

Add a Comment

Replies

You need to change to

$Calculated

and

$Calculator

But Float is not a text. So you should call

TextField("Calculated number", value: $Calculated, format: .number)

and

TextField("Calculator", value: $Calculator, format: .number)

But doing so, labels will not show.

This works better:

HStack {Text("Calculated number"); TextField("", value: $calculated, format: .number) }

and

HStack {Text("Calculator"); TextField("", value: $calculator, format: .number) }

Note also that var names should start with lowercase: calculated and calculator.

  • Other error: Cannot find ‘$calculated’ and ‘$calculator’

  • Normal, I changed to have first letter as lowercase (calculator vs Calculator, calculated vs Calculated). So, you need to change it everywhere in code.

  • I changed the first letter to lowercase, but it still show ‘didn’t find $calculator in scope’

Add a Comment