views:

35

answers:

1

I'm trying to take the output of arc4sin and put it into a label.

(EDIT: You can ignore this and just post sample code, if this is too irrelevant.)

I've tried:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *number =  [[NSString alloc] stringWithFormat: @"%@", arc4random() % 9];
    label.text = number;
}

I've created an IBOutlet for "label" and connected it. What's wrong here?

+1  A: 

In Cocoa format strings, %@ denotes an Objective-C object, which ints, floats, longs, and shorts all aren't. They are just C datatypes, and use their own formats, the exact same as in printf().

According to the manpage, arc4random() is defined as u_int32_t arc4random(void);, thus, you should use the %u format specifier (unsigned integer).

Edit: Thanks to Till for pointing this out: you want -initWithFormat: if you are calling -alloc, -stringWithFormat: is a class method of NSString.

chpwn
Nope, still crashes... There's probably something else wrong with my code. Any ideas?
Moshe
A UIAlertView works, something is up with my label or outlet... Thanks!
Moshe
You are sending 'stringWithFormat:' to an allocated object although it's a class method.
Till Theis