【问题标题】:Xcode iOS: Convert int to NSStringXcode iOS:将 int 转换为 NSString
【发布时间】:2012-03-08 14:16:27
【问题描述】:

我正在尝试从 UIPicker 中获取一个值,该 UIPicker 填充了整数、从 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); 

}

我在pickednumber = line 中收到错误:从'id' 分配给'int' 的整数转换指针不兼容。 我做错了什么?

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

【问题讨论】:

标签: ios xcode nsstring int


【解决方案1】:

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

这样做

pickednumber = [numbers intValue];

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

【讨论】:

    【解决方案2】:

    将整数转换为 NSString:

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

    将 NSString 转换为 int:

    int number = [string integerValue];
    

    【讨论】:

      【解决方案3】:
      NSString *intString = [NSString stringWithFormat:@"%d", myInt];
      

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

      【讨论】:

      • 标题不匹配。但很高兴 Giel 和 dotoree 同步并且问题得到了解决:)
      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 2010-11-25
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2011-01-24
      相关资源
      最近更新 更多