Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MickeyT

macrumors member
Original poster
Apr 26, 2010
92
0
Newcastle, United Kingdom
Hello all

What is the best code to use to test whether a text field contains any value? I've current got the following and its working, albeit I'm not sure if there is a better way:

Code:
if (purchasePrice.text==nil || purchasePrice.text.length==0)


Furthermore, if it does contain a value what is the best way to test to see if it is numeric?

Finally, and this is a question that is unrelated to the title but I just want to slip it in as well; I have the following statement to assign a Yes value to a boolean variable I have declared. After building my programme I have a warning against it which reads "Assignment makes pointer from integer without a cast":

Code:
errorFound=YES;

Does it have something to do with YES being the same as a 1 and NO being the same as 0?

Thanks for any assistance.
 
Furthermore, if it does contain a value what is the best way to test to see if it is numeric?
One thing you may want to consider is to change the keyboard type to prevent non-numerics from being entered in the first place.

Finally, and this is a question that is unrelated to the title but I just want to slip it in as well; I have the following statement to assign a Yes value to a boolean variable I have declared. After building my programme I have a warning against it which reads "Assignment makes pointer from integer without a cast":

Code:
errorFound=YES;

Does it have something to do with YES being the same as a 1 and NO being the same as 0?
No, it probably has to do with how you defined errorFound. Let me guess. Does it look like this?:
Code:
BOOL *errorFound;
 
No, it probably has to do with how you defined errorFound. Let me guess. Does it look like this?:
Code:
BOOL *errorFound;

To elaborate, BOOL is just a primitive and doesn't use a pointer

It should just be:

Code:
BOOL errorFound;
 
No, it probably has to do with how you defined errorFound. Let me guess. Does it look like this?:

Code:
BOOL *errorFound;

You're absolutely right - my mistake.

One thing you may want to consider is to change the keyboard type to prevent non-numerics from being entered in the first place.

I did set it to the numeric keyboard to begin with, but that doesn't have a decimal point button or a % sign, so I changed it to the one that did. Is there another keyboard type that will allow this. If not, is there something that can test for numeric values?

And is the test for a null string okay? I saw some stuff on the internet using NSScanner or something.
 
I did set it to the numeric keyboard to begin with, but that doesn't have a decimal point button or a % sign, so I changed it to the one that did. Is there another keyboard type that will allow this. If not, is there something that can test for numeric values?
Hmm, what kind of numerics allow a % sign? Anyways, this thread might help, especially the talk about numberFromString:.

And is the test for a null string okay?
Looks fine to me.
 
Hmm, what kind of numerics allow a % sign?

Actually that's a fair point. I was just thinking about how a user might type into the text field. The text field is for entering an interest rate and I thought a user might intuitively just type in "4%" rather than "0.04".

Might be better to have a label with '%' in it to the right of the textfield box and keep the field itself as numeric. I can then convert a "4" to "0.04" myself, and test to ensure the entered number never exceeds 100.

Cheers for the help on that one.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.