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

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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?

Yes. Until you learn bindings and value transformers, which will be some time yet.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,755
8,445
A sea of green
Code:
NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];

I am wondering. Is this common type casting code that is widely used?

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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
-intValue and -setIntValue: methods.
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,755
8,445
A sea of green
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".

Hence the value of reading class reference docs, rather than relying on youtube videos and inference.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.