Convert to NSString.

Discussion in 'Mac Programming' started by larswik, Jun 23, 2011.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Last night I tried my hand at Cocoa with a simple hello world button that displays that text in a NSTextFieldCell, neat.

    I then thought I would try to have code generate a random number and store it in a int variable and the display it in the text field instead. I got an error saying to the effect it was unable to assign an int to a pointer of NSString. Strings and int's are different types, I get this. But then I found this code online

    Code:
    NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
    I am wondering. Is this common type casting code that is widely used? Or is there a better way I should write this code? Here is the original code bellow

    Code:
    #import <Foundation/Foundation.h>
    
    @interface theDice : NSObject {
        
        int theRandomRollValue;
        IBOutlet NSTextFieldCell *lable;
    }
    - (IBAction)theButton:(id)sender;
    
    @end
    Code:
    #import "theDice.h"
    
    @implementation theDice
    
    - (IBAction)theButton:(id)sender {
        theRandomRollValue = arc4random() %100 +1;
     
         NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
        [lable  setStringValue:str];
    }
    @end
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    stringWithFormat: is fine and I would consider that normal.
     
  3. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    FWIW It's really not that different from an sprintf that you might use in straight C.

    B
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    One other way to do it is to use NSNumberFormatter like so:

    Code:
    NSNumber* num = [NSNumber numberWithInt: theRandomRollValue];
    NSString* str = 
      [NSNumberFormatter 
        localizedStringFromNumber:num
        numberStyle:NSNumberFormatterDecimalStyle
      ];
    
    It's more complex then using NSString stringWithFormat. But it has the advantage of formatting the number according to the user's Language & Text settings, such as putting in commas/points as appropriate.
     
  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    Cool, thanks. I am to the point now where I want to learn and also experiment. Experimenting helps me grasp the concepts I read about more.

    Dealing with any kind of text box on screen they display always string value? So everything needs to be converted to a string value to be displayed properly?

    Thank you.

    -Lars
     
  6. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    Yes. Until you learn bindings and value transformers, which will be some time yet.
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    FWIW, that's not type casting. It's a conversion, in the same way that sprintf() or sscanf() performs a conversion.


    I will also point out that NSTextField has a number of type conversion methods, which it inherits from NSControl. See the latter's class reference doc and look at -intValue and -setIntValue: methods. Also look at similar methods for float, double, etc. These methods are all conversions, not type casting.
     
  8. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    I did see those in a youtube videos I was watching. I first thought they meant "The initial values" when it was set up, and not 'Intiger values".

    Thanks!

    -Lars
     
  9. macrumors 603

    Joined:
    Aug 9, 2009
    #9
    Hence the value of reading class reference docs, rather than relying on youtube videos and inference.
     
  10. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    Anonymous Cocoa programmer quoted at the beginning of "Cocoa in a Nutshell".

    B
     

Share This Page