Hi thanks for the reply. I'm going to try it tonight.
Would you mind explaining the code a little bit? From my understanding, your looking at the length of the strings of each text box and then assigning a 1 or 0 depending on if it's empty or not. This is counting incrementally.
I've never seen syntax like that with the ? 1:0.
I also don't know what's going on here textField1.enabled = textField2.enabled
Some info would really help my understanding.
Thanks
Okay. It sounds like you have a limited knowledge of C. Obj-C is just an extension to C that adds a lot of support for objects and classes, so all valid C is also valid Obj-C.
First off, I should mention I had a typo. I originally had a variable called count but I decided to rename it nonEmpty and I forgot to replace count every time with it. Second, I added an initial assignment to the nonEmpty variable. Both of the changes are in my above post.
Next, I used three operators:
- += Increases the variable on the left by the amount on the right.
- (condition) ? (expression 1) : (expression 2) If condition is true (non-zero), returns expression 1. If condition is false (zero), returns expression 2.
- = Assigns the variable on the left with the amount on the right. EVALUATES FROM RIGHT TO LEFT.
These are all standard C operators. The lines that look like
Code:
nonEmpty += textField1.text.length ? 1 : 0;
are first looking at the length of the text in textField1. If the length of the text is zero, that is, if the field is empty, then the condition of the ?: operator is false, and it returns the value on the right of the colon, 0. This 0 is added to nonEmpty with the += operator, which, as you might know, will leave the value of nonEmpty unchanged (the field was empty, so that's the desired end result.)
If the length of the text is anything else, that is, if the field isn't empty, then the condition of the ?: operator is true, and it returns the value on the left of the colon, 1. This 1 is added to nonEmpty with the += operator, meaning nonEmpty will be incremented by 1 (the field wasn't empty, so that's the desired end result.)
I next utilized the fact that C (and Obj-C) treats all whitespace (that is, spaces, tabs, and returns) the same way. So I broke up the last expression into 3 lines, to hopefully make it easier to read (thus why I indented the second two lines of the expression and started them with the operator.)
textField's enabled property allows you to easily enable and disable text entry into a field by assigning it true (if it should be editable) or false (if it's shouldn't be editable.)
The = operator works from right to left, meaning you should look at what comes after the final = in a chain of ='s to see what everything is being assigned to. In
i is being assigned zero. In
a, b, and c are all being assigned to 0.
So after the final = in my code I have
Code:
nonEmpty < 2 ? TRUE : FALSE;
If nonEmpty is less than 2, it'll return what's on the left of the colon, TRUE, and assign that to each of the four textField's enabled properties (it'll enable all textFields if only one or zero contain text.) If nonEmpty is 2 or greater, it'll return what's on the right of the colon, FALSE, and assign that to each of the four textField's enabled properties (it'll disable all textFields if two or more contain text.)
Technically, the final ?: isn't really necessary and you could just have
as the final line, because that will return 1 (TRUE) if it's true or 0 (FALSE) if it's false, but it helps improve the readability of the code a bit, I think.