代码之家  ›  专栏  ›  技术社区  ›  Mark

NSNumber intValue赋予EXC\U BAD\U访问权限

  •  0
  • Mark  · 技术社区  · 15 年前

    我通过第三方API传递了一个NSNumber,当我调用索引上的intValue时,我得到一个EXC\u BAD\u访问错误:

    -(CPFill *) barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSNumber *)index; 
    {
        NSLog(@"bar index %i", index);
        int value = [index intValue];
    }
    

    我在调试器中得到的输出是:

    条形图索引0

    条形图索引1

    到底怎么回事?

    我注意到第一次调用这个方法时,索引是 但下一次显然不是。。。

    3 回复  |  直到 15 年前
        1
  •  6
  •   Chuck    15 年前

    一个快速的谷歌出现了 a framework NSUInteger 作为论据。假设这是有问题的库,您将参数错误地键入为 NSNumber* . 当你认为你看到 nil 对于索引,实际上只看到索引0。

        2
  •  2
  •   Brian    15 年前

    可能是API出了问题,或者您使用了错误的方法签名。索引应该是原始类型吗,比如NSInteger?

        3
  •  1
  •   Kurbz    15 年前

    尝试使用 %@ 用于打印 index ,自 指数 %d 具有 [index intValue]

    -(CPFill *) barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSNumber *)index 
    {
        NSLog(@"bar index %@", index);
        //or NSLog(@"bar index %d", [index intValue]);
        int value = [index intValue];
    }
    

    编辑:顺便说一句,在方法标题的末尾有一个分号