Get a text UITextField, convert, sum and set again

Discussion in 'iOS Programming' started by art0071, May 20, 2009.

  1. art0071 macrumors newbie

    Joined:
    May 20, 2009
    #1
    Hello,

    In my first app, i need get a text from UITextField, convert and set into UitextField.

    My code is:

    int *valorcinq = (int) [campoHoraCinq text];
    int *valorcem = (int) [campoHoraCem text];

    valorcinq = valorcinq + valorcem; --> Error: invalid operands to binary +

    :(

    Tks for help-me
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Why are you declaring valorcinq and valorcem as pointers?
     
  3. art0071 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #3
    I thought it was necessary to declare a variable with *.
    For basic types like int, double, etc. need not be declared as pointer?

    Sorry if the question is simple, I'm starting to look more objective-c now.

    Thanks for Help
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    No, objects are declared with *. Basic types are not objects.

    I think you might wanna step away from the real code for a bit and go review the basics of Objective-C, as well as its interaction with C.
     
  5. art0071 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #5
    I'm watching the podcast and I'm even more in the beginning.

    I have one more question:
    When I do the command;

    (int) valorcinq int = [campoHoraCinq text];
    NSLog (@ "Val is:% i", valorcinq);

    Within the field campoHoraCinq time I put the value 2, most log me in
    returns another value (4782560). Why?

    Thanks
     
  6. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #6
    Try this:
    Code:
    NSLog([NSString stringWithFormat:@"Val is:% d", valorcinq]);
    

    *edit*
    Didn't even catch what dejo pointed out below. The code you have, you're not even going to get the number out of it. You need to get the int value of the string first. This is programming 101 stuff here for the most part.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    I take it you are intending to convert the text field into an int, with this line?:
    Code:
    valorcinq int = (int) [campoHoraCinq text]; 
    If so, you can't just cast an NSString (presumably that's what [campoHoraCinq text] returns, right?) into an int. Check the class reference for NSString and look around the int... instance methods it has.
     
  8. art0071 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #8
    I do not understand why it seems easy to me get a field, put in a variable and display on screen.

    Find that the value is wrong after making the conversion to int, using this command:

    valorcinq int = (int) [campoHoraCinq text];

    Pordo someone please explain me what's wrong?

    I tried to use the NSLog ([NSString stringWithFormat: @ "Val is:% d", valorcinq]), plus the value is already been printed the wrong conversion.

    Thanks again
     
  9. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #9
    NSString is an object. an object is just that - an object. you cannot convert it to an int.
    look at it this way: would you expect a result if you converted a UIView into an int, or a UILabel? Nope.
    an object is a pointer to a memory adress. and you cannot convert memory adresses.

    If you look at the reference of NSString though, you will see that NSString has a method "intValue" which returns the text of the NSString as an int. This is what you need.
     
  10. art0071 thread starter macrumors newbie

    Joined:
    May 20, 2009
    #10
    Hello BlacWolf,

    Thanks for the explanation.
    Now I understand the problem and could solve my code.

    Below the code if someone who is starting out;

    Code:
    NSString *strValor = [campoHoraCinq text];
    int valorConvertido = [strValor intValue];
    
    NSLog(@"Campo: %i", valorConvertido);
    
    Thanks for the help of all;)
     
  11. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #11
    yep, that's the way to do it.
    btw, you can also use
    Code:
    int valorConvertido = [[campoHoraCinq text] intValue];
    
     

Share This Page