Convert to NSString.

macrumors 68000

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

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.

macrumors 603

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.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.