[英]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. 我试图从UIPicker中获取avalue,其中包含圆形数字,从中抽取的整数和NSMutableArray。 I'm trying to convert the pulled values to an actual int. 我正在尝试将拉出的值转换为实际的int。
I tried this in the .m: 我在.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'. 我在picknumber = line中得到错误:指向整数转换的不兼容指针从'id'分配给'int'。 What am i doing wrong? 我究竟做错了什么?
Message was edited by Sounddesigner on 3/8/12 at 3:05 PM 消息由Sounddesigner于3月8日下午3:05编辑
NSString
has a convinience method to get integer value of a text NSString
有一个方便的方法来获取文本的整数值
do this 做这个
pickednumber = [numbers intValue];
NSLog(@"Picked number %d ", numbers); // not %@ ..%@ is for objects .. not int
To convert integer to NSString: 要将整数转换为NSString:
NSString *string = [NSString stringWithFormat:@"%i",integerNumber];
To convert NSString to int: 要将NSString转换为int:
int number = [string integerValue];
NSString *intString = [NSString stringWithFormat:@"%d", myInt];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.