views:

429

answers:

2

Y'hello.. Yep, it's me again, sorry.

I am trying to .. um, see the code, you'll understand. If not, I'll explain later.

// When row is selected
- (void)pickerView:(UIPickerView *)pickerTimer didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    switch (row) {
        case 0:
            NSLog(@"It obviously worked.0");
            break;
        case 1:
            NSLog(@"It obviously worked.1");
            break;
        case 2:
            NSLog(@"It obviously worked.2");
            break;
        case 3:
            NSLog(@"It obviously worked.3");
            break;
        case 4:
            NSLog(@"It obviously worked.4");
            break;
        case 5:
            NSLog(@"It obviously worked.5");
            break;
        case 6:
            NSLog(@"It obviously worked.6");
            break;
        default:
            NSLog(@"It did kindof work.NIL");
            break;
    }
}

I guess what I want to know is, is there any way of simplifying this? Just do like,

NSLog(@"It did work! %@", row);

For the record, I tried that, and it did not work.

Thank you. I am a n00b at iPhone programming :)

+7  A: 

The format specifier for an integer is %d%@ specifies an object. To be safe, you should also cast the NSInteger to an int when you pass it to printf() (so you would write @"%d", (int)row), because the size of NSInteger is not guaranteed to be the size the %d specifier tells printf() to expect.

Incidentally, you don't need to repeat it for each case statement. Without a break, control will fall through to the next case.

Chuck
Hah, I'm such a noob..Is there any overview over such things as format specifiers somewhere?Thank you :)
Emil
Just do a search on C printf, it'll not list %@ but it has everything else.
Kendall Helmstetter Gelner
@Emil: This link contains all supported format specifiers for Core Foundation: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265.
dreamlax
Chuck: According to @dreamlax's link, you ought to cast NSInteger to `long` instead.
Jeff Kelley
+5  A: 

Like Chuck said you can do...

NSLog(@"It did work! %d", row);

...or you can get fancy and turn it into an object...

NSLog(@"It did work! %@", [NSNumber numberWithInt:row]);
TechZen