使用我自己的 typedef 枚举,获取“无需强制转换即可从整数生成指针”
我设置的错误是“赋值使指针来自整数而不进行强制转换”。这是我的代码
typedef enum {
UIIconTypeCustom = 0,
UIIconTypeStandard,
} UIIconType;
:
-(void)addIconWithType:(UIIconType *)iconType {
...
}
这是有问题的一行:
[iconView addIconWithType:UIIconTypeStandard];
The error i'mg etting is 'Assignment makes pointer from integer without cast'. Here is my code:
typedef enum {
UIIconTypeCustom = 0,
UIIconTypeStandard,
} UIIconType;
.
-(void)addIconWithType:(UIIconType *)iconType {
...
}
And this is the line it has a problem with:
[iconView addIconWithType:UIIconTypeStandard];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的方法有一个指针作为参数:
并且您向它传递一个整数:
将您的方法定义更改为:
此外,不要定义您自己的枚举、类或任何带有前缀
用户界面
。该前缀是苹果公司保留的,使用它会给你自己带来不必要的麻烦。使用您的姓名缩写或您的公司或项目的姓名缩写。几乎所有 Objective-C 方法都具有带有星号的参数的原因是,当您使用对象时,该对象保留在内存中的一个位置,并且您只需传递指向该对象的指针。这就是
MyClass *
所表示的——指向MyClass
类型的对象的指针。指针为您提供了对象在内存中位置的地址,这样您就可以避免在要使用它时将整个对象从一个地方移动到另一个地方。在这种情况下,您想要传递给方法的东西不是一个对象,而是一个简单的整数,因此您可以直接传递该整数。Your method has a pointer as parameter:
and you are passing it an integer:
Change your method definition to this:
Also, don't define your own enums, classes, or anything else with the prefix
UI
. That prefix is reserved by Apple and you will cause pointless headaches for yourself by using it. Use your initials or the initials of your company or of the project.The reason that almost all Objective-C methods have parameters with the asterisk is that when you are using an object, the object stays in one place in memory, and you just pass around a pointer to that object. That's what
MyClass *
indicates -- a pointer to an object of typeMyClass
. The pointer gives you the address of the object's location in memory, so that you can avoid having to move the entire object from place to place when you want to use it. In this case, the thing that you want to pass to the method isn't an object, but a simple integer, so you can just pass the integer directly.更改
为:
您的方法需要一个指向 UIIconType 的指针,但您试图按值传递它。
Change:
to:
Your method expects a pointer to a UIIconType, but you are trying to pass it by value.