UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SOLVED day 19: how do i compare the user's input to the conversion options?

Forums > 100 Days of SwiftUI

probably a stupid question, but here it goes. i'm working on the unit conversion challenge and trying to create a computed property that will set all possible user input to a base value (except input already of that value) that i can then convert to the desired unit. my first insntinct here is to use if statements to determine how to convert it; however, when i try and type something like "if $conversionOptions == "Farenheit"", i get this error: Referencing operator function '==' on 'StringProtocol' requires that 'Binding<[String]>' conform to 'StringProtocol'

any other way i think to solve this returns errors as well, does anyone know how to best approach this? thank you

2      

@angie asks a great question!

probably a stupid question, but here it goes.....
[.... snip ..... ]. does anyone know how to best approach this?

Logic is part of the fun of developing a solution!

Your approach seems to be: take 32 degrees farenheit and convert it to a base value (for example: Schrute Bucks). Then when the user selects a different measurement (Stanley nickels, Kelvin, or centigrade) you'll be ready to convert the base value to the desired measurement. Good thinking!

But then you get an error that throws you off track.

Referencing operator function '==' on 'StringProtocol'
requires that 'Binding<[String]>' conform to 'StringProtocol'

What in rain-drenched Wales does that even mean?

The key here is Binding<String>.

Two Way Pipe

Think of the dollar sign as a two way pipe to your variable. In one direction you can read the value. In the other direction you can write the value. You need a two way binding in user interface objects like a Picker. Why? Because you want to display the variable IN the picker, but when your user selects a different option, you want to write that NEW option back to the variable.

See? It's a two way binding.

But in your IF statement, you will NOT be writing to the variable. You only need to read the variable. Consequently, the dollar sign is frivilous, bogus, and completely unnecessary. This is why the compiler complains. I agree with what you're thinking right now. YES, the compiler should just say, "Hey Angie! You don't need the $ in your IF statement."

Keep Coding

Please return here with more questions. Also would like to know how you completed this logic.

Stupid Questions

PS: Many people might say, "There is no such thing as a stupid question!" However, I might counter with "They not met all of my co-workers." (Another Office show reference! (US version))

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.