Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode iOS: Convert int to NSString

I'm trying to grab avalue from a UIPicker which is populated with round numbers, integers pulled from and NSMutableArray. I'm trying to convert the pulled values to an actual int.

I tried this in the .m:

int pickednumber;


......

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{


NSString *numbers = [arrayNumbers objectAtIndex:[pickerView selectedRowInComponent:0]];

pickednumber = [NSString stringWithFormat:@"%d", numbers];

NSLog(@"Picked number %@ ", numbers); 

}

I get the error in the pickednumber = line: Incompatible pointer to integer conversion assigning to 'int' from 'id'. What am i doing wrong?

Message was edited by Sounddesigner on 3/8/12 at 3:05 PM

like image 475
Giel Avatar asked Mar 08 '12 14:03

Giel


3 Answers

NSString has a convinience method to get integer value of a text

do this

pickednumber = [numbers intValue];

NSLog(@"Picked number %d ", numbers);  // not %@ ..%@ is for objects .. not int
like image 191
Shubhank Avatar answered Nov 04 '22 09:11

Shubhank


To convert integer to NSString:

NSString *string  = [NSString stringWithFormat:@"%i",integerNumber];

To convert NSString to int:

int number = [string integerValue];
like image 44
ak_tyagi Avatar answered Nov 04 '22 09:11

ak_tyagi


NSString *intString = [NSString stringWithFormat:@"%d", myInt];

http://forums.macrumors.com/showthread.php?t=448594

like image 2
dotoree Avatar answered Nov 04 '22 11:11

dotoree