Xcode iOS:将int转换为NSString

时间:2012-03-08 14:16:27

标签: ios xcode nsstring int

我正试图从UIPicker中获取avalue,其中包含圆形数字,从中抽取的整数和NSMutableArray。我正在尝试将拉出的值转换为实际的int。

我在.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); 

}

我在picknumber = line中得到错误:指向整数转换的不兼容指针从'id'指定'int'。 我做错了什么?

消息由Sounddesigner于3月8日下午3:05编辑

3 个答案:

答案 0 :(得分:96)

NSString方便方法来获取文字的整数值

这样做

pickednumber = [numbers intValue];

NSLog(@"Picked number %d ", numbers);  // not %@ ..%@ is for objects .. not int

答案 1 :(得分:3)

将整数转换为NSString:

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

将NSString转换为int:

int number = [string integerValue];

答案 2 :(得分:2)

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

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