【问题标题】:NSInteger to NSString mis understoodNSInteger 到 NSString 被误解了
【发布时间】:2014-10-03 12:59:19
【问题描述】:

我有一个关于NSIntegerNSString 的问题 我一直在尝试,但没有运气。 但是出于某种奇怪的原因,我的NSInteger 之一确实如此,但我不知道为什么 我有这个方法:

//Lets say loc_id = 869
-(void)addResource:(NSInteger)loc_id
{
   //[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
   NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);
}

结果不好

因为它在xcode的调试器中给了我:addResource: loc_id:20987787696

应该是loc_id:860

当我用loc_id = 8 尝试这个时 它工作得很好为什么会出现这种奇怪的行为 或者我误解了什么感谢您的提示和帮助。 当我以字符串格式使用%@ 时,它适用于loc_id = 8,但是当我尝试使用时 %d 这是正常使用,它也会导致loc_id:20987787696 怎么来的?

方法的使用:

-(IBAction)switchChanged:(id)sender {
  UISwitch *switchControl = sender;
  [loc addResource:switchControl.tag] 
 }

 -(void)addSwitch:(NSInteger)aid
 {
    UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(x,y,w,h)]];
    sw.tag = aid;
    [sw addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEvenValueChanged]];
    [self.View addSubview:sw];
 }

 //Data is dynamic (NSObject) but for example i will exclude the dynamic data.
 for(int i = 0; i < 20; i++)
 {
   NSInteger *loc_id = 868;
   [self addSwitch:loc_id];

 }

在某个时候,我想将它插入到 NSMutableArray 中,如上面注释的那样,但我希望它的值为 869,它让我感到 2071894069 我想要 int 的值而不是内存的值

亲切的问候, 斯蒂芬

【问题讨论】:

  • 使用 %i 代替 %@ !
  • 也试过不行
  • 我也得到 loc_id:20987787696
  • 问题是格式中的%@ 会打印指向整数的点,而不是整数本身。正如@rdurand 所说,使用%i
  • NSInteger 是一个长值。您可以通过 cmd 单击 NSInteger 符号来检查这一点

标签: ios objective-c xcode xcode6


【解决方案1】:

您应该使用%li@ld 而不是%@,并在NSLog 中删除stringWithFormat: 调用(它已经需要一种格式):

NSLog(@"addResource: loc_id:%li",loc_id);

%@ 是格式化对象描述和字符串的选择器。 %li 是将整数转换为长整数,并防止最终的编译器警告。


编辑:

在评论中提出了很多问题之后,您的代码中还有一个错误:

NSInteger *loc_id = 868;

您不应将loc_id 作为指针启动。改用:

NSInteger loc_id = 868;

编辑#2:

正如聊天中所讨论的,问题来自 NSInteger 值是从其所在的数组/字典中提取的方式。

【讨论】:

  • @StefanvandeLaarschot 什么不起作用,这段代码是正确的。
  • @StefanvandeLaarschot 确保您确实使用有效的 NSInteger 值以及您期望的值调用该方法。
  • @StefanvandeLaarschot 请阅读 rdurand 发布的答案。您将看到您删除了[NSString stringWithFormat:。这是因为NSLog 已经接受了一种格式。
  • 我仍然希望值 869,但它让我失望 20987787696
  • 设置标签时确保aid有预期值
【解决方案2】:

要显示你的 NSInteger,你不需要在 NSString 中格式化它。 只需使用下面的代码即可。

NSLog(@" %ld",(long)loc_id);

【讨论】:

    【解决方案3】:

    对于整数,您需要使用 %ld 或 %li。 %@ 用于对象。

    NSLog([NSString stringWithFormat:@"addResource:loc_id:%ld",(long)loc_id]);
    

    【讨论】:

      【解决方案4】:

      您还可以将您的 Integer 转换为 long 值:

      -(void)addResource:(NSInteger)loc_id
      {
          NSLog(@"addResource: loc_id:%ld",(long)loc_id);
      }
      

      【讨论】:

      • 不要在NSLog中使用stringWithFormat:
      【解决方案5】:
          NSLog([NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id]);
      

      收到警告:格式字符串不是字符串文字。

      以下代码按顺序运行:

      -(void)addResource:(NSInteger)loc_id
      {
          //[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
          NSString *string = [NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id];
          NSLog(@"%@", string);
      }
      

      【讨论】:

      • 为什么不直接将格式和参数提供给NSLog,就像 rdurand 的回答所说的那样。
      • 有一点我想将它插入到 NSMutableArray 中,如上面注释的那样,但我希望值为 869,它让我感到 2071894069 我想要 int 的值而不是内存的值
      【解决方案6】:

      苹果文档

      实用函数NSLog()NSLogv() 使用NSString 用于记录错误消息的字符串格式化服务。请注意,作为 因此,在指定参数时应该小心 对于这些功能。一个常见的错误是指定一个字符串 包括格式字符,如下例所示。

      错误

      你不需要在 NSLog 中添加格式化字符串:

      NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);
      

      同样在 64 位 上使用 %li 提供整数精度,如 @rdurand asnwer

      NSLog(@"addResource: loc_id:%li",loc_id);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-19
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2011-12-04
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多