Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Help with if Statement with type Double and textfields

let total2 = Double(TextField2.text!)
let total3 = Double(TextField3.text!)

            if ((total2 >= 0 || TextField2 == nil) && (total3 >= 0 || TextField3 == nil)) {
                TextField2Total = Double(TextField2.text!)!
                TextField3Total = Double(TextField3.text!)!
                //print(TextField1Total)
                print(TextField2Total)
                print(TextField3Total)
                print(TextField2.text)
                print(TextField3.text)
            } else {
                print("none")
            }

When i run my code it only prints TextFieldTotal2 and TextFieldTotal3 when both textfields have a number greater than or equal to 0. But it one or both textfield have no text aka nil it prints nothing. Can Someone try this in xcode and help me fix my problem please.

Hey :

What are you trying to achieve ?

When you say: But it one or both textfield have no text aka nil it prints nothing. What do you mean ? If there is no text in the text field, then it will not print any text.

Please try to explain what you are trying to do here, and what is the result that you need. This way I may be able to help

Thank you

1 Answer

So for textfield2 and textfield3, if they both have numbers entered it prints the numbers, if only one textfield has a number and the other has nothing entered then nothing is printed. i need it to print for all cases, when both have numbers entered, when only one has a number entered or if both have nothing entered.

I hope that makes sense