【问题标题】:ios NSUinteger to type 'id'ios NSUinteger 输入“id”
【发布时间】:2016-04-10 00:40:23
【问题描述】:

今天我的大脑让我失望了。我知道这一定很简单,但我就是没看到。

CGFloat *minutes = [self.displayData objectForKey:index];

不兼容的整数到指针的转换将“NSUInteger”(又名“unsigned int”)发送到“id”类型的参数

index 是循环中的NSUInteger,(0 1 2 3 等)

我该如何克服这个问题?谢谢。

【问题讨论】:

  • objectForKey: != objectAtIndex:

标签: ios


【解决方案1】:

字典等待对象作为键 (id) 而不是普通整数 (NSUInteger)。

尝试将整数包装在 NSNumber 对象中。

CGFloat *minutes = [self.displayData objectForKey:[NSNumber numberWithUnsignedInteger:index]];

【讨论】:

    【解决方案2】:

    -objectForKey: 方法返回一个 id 类型的值,但您试图将其分配给 CGFloat*,它与 id 不兼容,因为 CGFloat 不是一个类。如果您知道您从字典中检索的对象是某种类型,例如 NSNumber,您可以在获取时采取措施将其转换为 CGFloat。

    另外,您正在尝试使用 int 作为字典的键,但字典也要求它们的键也是对象。如果您想通过索引访问对象,请将它们存储在数组而不是字典中。

    把它们放在一起,你会得到这样的东西:

    // after changing the displayData to an array
    NSNumber *number = [self.displayData objectAtIndex:index];
    CGFloat minutes = [number floatValue];
    

    【讨论】:

      【解决方案3】:

      你不应该使用这样的指针:

      CGFloat minutes = [self.displayData objectForKey:index];
      

      【讨论】:

        【解决方案4】:

        简单的问题。只是不要使用 NSUIntegrer。代替它,请执行以下操作:

        id index;//then set the index
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-18
          • 1970-01-01
          相关资源
          最近更新 更多