Set text of label to integer

Discussion in 'iPhone/iPad Programming' started by RutgerB, Jul 14, 2008.

  1. macrumors newbie

    Joined:
    Jul 13, 2008
    #1
    Hi

    I want to display the value of an integer in a label.

    I have this code to display a string in a label:
    Code:
    [solution setText:@"Hello"];
    
    So, solution is the name of the label.

    I want to display the value of the integer variabel called 'solution' in the label.

    I know it's a very beginners question, so I was wondering if there's a good place to find simple coding things like this. I tried the Xcode help but I always find a bunch of articles about everything except what I'm looking for :/


    Thanks for helping me!
    -Rutger
     
  2. macrumors 65816

    dgdosen

    Joined:
    Dec 13, 2003
    Location:
    Seattle
    #2
    You could put it in an NSNumber and just get the description.

    In my opinion, there's so much moving pointers of data around from page to page, it makes sense to bite the bullet and use NSNumber instead of raw C scalar types for numbes... Handle them right, and things just work. You don't have to worry about losing them from page to page. Just clean up after yourself.

    That said, I'm a newb to Objective C, and doing math with NSNumber types is a beeyotch...
     
  3. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #3
    Thanks for your reply!

    Could you perhaps post a little code snippet of simply how to assign such a NSNumber?

    I found this in Xcode help:

    Code:
    + (NSNumber *)numberWithInteger:(NSInteger)value
    But what can I do with this :/
     
  4. macrumors member

    Joined:
    May 24, 2003
    #4
    An easy way to do this would be to use an old-fashioned formatted string to convert your int to an NSString.

    Try this (I'm calling the label that will display the value 'solutionLabel', and the integer 'solution', to try and make things clearer:

    Code:
    NSString *theValue = [NSString stringWithFormat:@"%d", solution];
    solutionLabel.text = theValue;
    It's a bit of a 'cheat', but it should work.
     
  5. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #5
    Thanks for your help.

    I have this code now:
    Code:
    int solution = 8;
    NSString *theValue = [NSString stringWithFormat:@"%d", solution];
    This is just for assigning 8 to the string theValue. But it gives me this error:
    initializer element is not a constant.
     
  6. macrumors member

    Joined:
    May 24, 2003
    #6
    That's strange. Where does XCode report the error?

    The code you're using compiles OK for me.

    By the way, if you really want to use NSNumber, you could try:

    Code:
    NSNumber *theSolution = [NSNumber numberWithInt: solution];
    solutionLabel.text = [theSolution stringValue];
     
  7. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #7
    It reports the error at the second line of my code snippet. I've had this error once before.. maybe something's wrong somewhere else.

    I'm new to developing for the iPhone (as you could have guessed :D) so probably I did something stupid.

    I have experience with c# and ActionScript but seems like this is something a little more difficult. But it's a nice challenge for spending the summer so I'm very glad you guys are helping me :)
     
  8. macrumors member

    Joined:
    May 24, 2003
    #8
    Do you by any chance have this piece of code outside of the @implementation block? If you do, the compiler will consider that you are trying to initialise a global variable using another variable (ie, not a constant). That's a no-no, so it will give you this error. If you ensure that the code falls inside a method that is inside the @implementation block, then I believe that it *should* (note that I'm not saying *will* :)) compile correctly.
     
  9. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #9
    Yeah, it was but putting the code inside the @implementation block doesn't stop the error. This is my complete code of the class that does the calculations (I'm trying to make a calculator)

    I have a label called 'uitkomst' and 6 buttons (een, twee, drie, vier, vijf and zes; the dutch words for 1 to 6). (I am planning on adding 7,8,9 and 0 :) ).

    Code:
    
    #import "RMBerekeningen.h"
    
    
    
    @implementation RMBerekeningen
    
    int solution = 8;
    NSString *theValue = [NSString stringWithFormat:@"%d", solution];
    
    - (IBAction)een:(id)sender {
        [uitkomst setText:@"1"];
    }
    
    - (IBAction)twee:(id)sender {
        [uitkomst setText:@"2"];
    }
    - (IBAction)drie:(id)sender {
        [uitkomst setText:@"3"];
    }
    - (IBAction)vier:(id)sender {
        [uitkomst setText:@"4"];
    }
    
    - (IBAction)vijf:(id)sender {
        [uitkomst setText:@"5"];
    }
    
    - (IBAction)zes:(id)sender {
        [uitkomst setText:@"6"];
    }
    @end

    I'm just wondering: is it possibly that I need to change something in the .h file? The .h file looks like this:

    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface RMBerekeningen : NSObject {
        IBOutlet id uitkomst;
    }
    - (IBAction)een:(id)sender;
    - (IBAction)drie:(id)sender;
    - (IBAction)twee:(id)sender;
    - (IBAction)vier:(id)sender;
    - (IBAction)vijf:(id)sender;
    - (IBAction)zes:(id)sender;
    @end
    
     
  10. macrumors member

    Joined:
    May 24, 2003
    #10
    The line that starts NSString *must* be inside a method. That is what is causing the error.

    For test purposes, try adding the offending line to one of the methods that you already have, or maybe make a new method for, say the "=" key:

    Code:
    - (IBAction)evenaart:(id)sender {
         [uitkomst setText:[NSString stringWithFormat:@"%d", solution]];
    }
    (Sorry if that's the wrong word for "equals" - my Dutch is a bit rusty...:))
     
  11. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #11
    Thanks, the erros stopped :).

    I have this code now that is executed when you touch a button "een".

    Code:
    - (IBAction)een:(id)sender {
    	NSNumber *solution = [NSNumber numberWithInt:1 ];
    	[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
    }
    The result shown in labelUitkomst is: "number is 4245024". Wow :confused:
    I just want it to be "number is 1"
     
  12. macrumors member

    Joined:
    May 24, 2003
    #12
    That's because you're using a mixture of the two suggestions I gave you. If you want to use an NSNumber, you can just use NSNumber's stringValue: method to return you an NSString representation of its value:

    Code:
    - (IBAction)een:(id)sender {
    	NSNumber *solution = [NSNumber numberWithInt:1 ];
    	[labelUitkomst setText:[NSString stringWithFormat:@"number is %@", [solution stringValue]]];
    }
    Otherwise, if you just declare solution to be an int rather than an NSNumber, it should just work:

    Code:
    - (IBAction)een:(id)sender {
    	int solution = 1;
    	[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
    }
     
  13. thread starter macrumors newbie

    Joined:
    Jul 13, 2008
    #13
    Thank you very much it works!

    I'm now going to read some more on objective c, so I don't have to keep on asking dumb questions :rolleyes:
     
  14. macrumors member

    Joined:
    May 24, 2003
    #14
    Excellent! I'm glad you're up and running.
    Objective-C is very straightforward, really, but you're bound to have a few problems as you learn it. Apple's documentation is generally very good, though, and they provide lots of examples, so it's not too difficult to figure things out once you get the idea of how things work.
    If you're looking for a general book on programming using Objective-C, I recommend "Programming in Objective-C" by Stephen Kochan. It's not iPhone (or even Mac) specific, but it is a very clear introduction to the language. There's a new edition, covering Objective-C 2.0, coming out at the end of the year, if you're prepared to wait.
     

Share This Page