【问题标题】:Incompatible integer to pointer conversion assigning to 'int *' from 'int'从“int”分配给“int *”的不兼容整数到指针转换
【发布时间】:2011-07-14 16:26:18
【问题描述】:

我还有另一个讨厌的警告,我想离开。基本上,我有一个这样声明的 int:@property (nonatomic, assign) int *myInt; 并设置如下:myInt = 0;。它也在实现文件中综合。我在设置 int 值的那一行收到警告,上面写着 Incompatible intiger to pointer conversion assigning to 'int *' from 'int'. 我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: ios objective-c xcode pointers warnings


    【解决方案1】:

    错误信息中有很大的提示!

    在 C 和 Objective C 中,int 是一种原始数据类型。你写了int *,意思是“一个指向int的指针”,而看起来你只是想要一个int。

    所以把你的属性改成这样:

    @property (nonatomic, assign) int myInt;
    

    更多信息,谷歌“C指针”,你会发现这样的信息:http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    【讨论】:

      【解决方案2】:

      是的,只需从声明中删除星号 (*)。

      例如,声明 BOOL isChecked 而不是 BOOL * isChecked

      【讨论】:

        【解决方案3】:

        解决此问题的另一种方法是不声明指针(去掉星号):

        @interface ViewController : UIViewController {
            NSUInteger myObjcInt;  // unsigned ( >= 0 ) NSObject int, yo
        }
        

        【讨论】:

        • 这不是另一种方式——这与 oculus 所说的相同。顺便说一句,您的评论称其为“NSObject int”,作为一个术语并没有多大意义。
        • 触摸。我在搜索与 NSInteger 相关的问题时阅读这篇文章,而不是 Int。伙计们,那里的回复有点太快了。我的错。
        【解决方案4】:

        int *MyInt 是指向 int 的指针,而不是 int。
        正如其他人告诉你的那样,只需删除*,你就会有一个常规的int。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-16
          • 1970-01-01
          相关资源
          最近更新 更多