Xcode iOS:将 int 转换为 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9619019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:13:41  来源:igfitidea点击:

Xcode iOS: Convert int to NSString

iosxcodensstringint

提问by Giel

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.

我正在尝试从 UIPicker 中获取一个值,该 UIPicker 填充了整数、从 NSMutableArray 中提取的整数。我正在尝试将拉取的值转换为实际的整数。

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'. What am i doing wrong?

我在 picknumber = 行中收到错误:Incompatible pointer to integer conversionassigning to 'int' from 'id'。我究竟做错了什么?

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

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

采纳答案by dotoree

回答by Shubhank

NSStringhas a conviniencemethod to get integer valueof a text

NSString有一个方便的方法来获取文本的整数值

do this

做这个

pickednumber = [numbers intValue];

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

回答by ak_tyagi

To convert integer to NSString:

要将整数转换为 NSString:

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

To convert NSString to int:

要将 NSString 转换为 int:

int number = [string integerValue];