Testing for numbers/null strings

Discussion in 'iOS Programming' started by MickeyT, May 4, 2010.

  1. MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    One thing you may want to consider is to change the keyboard type to prevent non-numerics from being entered in the first place.

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

    ulbador

    Joined:
    Feb 11, 2010
    #3
    To elaborate, BOOL is just a primitive and doesn't use a pointer

    It should just be:

    Code:
    BOOL errorFound;
     
  4. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #4
    You're absolutely right - my mistake.

    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Hmm, what kind of numerics allow a % sign? Anyways, this thread might help, especially the talk about numberFromString:.

    Looks fine to me.
     
  6. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #6
    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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You're welcome. And I like the way you're thinking with the % label and such. Shows that you are thinking from the users' perspective. That's a good sign.
     

Share This Page