PDA

View Full Version : Convert to NSString.




larswik
Jun 23, 2011, 05:22 PM
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

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

#import <Foundation/Foundation.h>

@interface theDice : NSObject {

int theRandomRollValue;
IBOutlet NSTextFieldCell *lable;
}
- (IBAction)theButton:(id)sender;

@end

#import "theDice.h"

@implementation theDice

- (IBAction)theButton:(id)sender {
theRandomRollValue = arc4random() %100 +1;

NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
[lable setStringValue:str];
}
@end



robbieduncan
Jun 23, 2011, 05:23 PM
stringWithFormat: is fine and I would consider that normal.

balamw
Jun 23, 2011, 05:24 PM
FWIW It's really not that different from an sprintf that you might use in straight C.

B

jiminaus
Jun 23, 2011, 05:45 PM
One other way to do it is to use NSNumberFormatter like so:


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
Jun 23, 2011, 06:17 PM
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
Jun 23, 2011, 06:41 PM
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
Jun 23, 2011, 08:08 PM
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
Jun 23, 2011, 08:30 PM
-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
Jun 24, 2011, 01:02 AM
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.

balamw
Jun 24, 2011, 05:26 AM
Hence the value of reading class reference docs, rather than relying on youtube videos and inference.

You don't remember Cocoa; you look it up!

Anonymous Cocoa programmer quoted at the beginning of "Cocoa in a Nutshell (http://oreilly.com/catalog/9780596004620)".

B