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
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
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