使用我自己的 typedef 枚举,获取“无需强制转换即可从整数生成指针”

发布于 2024-11-07 15:55:40 字数 328 浏览 0 评论 0原文

我设置的错误是“赋值使指针来自整数而不进行强制转换”。这是我的代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2024-11-14 15:55:40

您的方法有一个指针作为参数:

-(void)addIconWithType:(UIIconType *)iconType;
// Takes a pointer to a UIIconType as argument

并且您向它传递一个整数:

[iconView addIconWithType:UIIconTypeStandard];
// UIIconTypeStandard is not a pointer to a UIIconType

将您的方法定义更改为:

-(void)addIconWithType:(UIIconType)iconType;

此外,不要定义您自己的枚举、类或任何带有前缀 用户界面。该前缀是苹果公司保留的,使用它会给你自己带来不必要的麻烦。使用您的姓名缩写或您的公司或项目的姓名缩写。

几乎所有 Objective-C 方法都具有带有星号的参数的原因是,当您使用对象时,该对象保留在内存中的一个位置,并且您只需传递指向该对象的指针。这就是MyClass * 所表示的——指向MyClass 类型的对象的指针。指针为您提供了对象在内存中位置的地址,这样您就可以避免在要使用它时将整个对象从一个地方移动到另一个地方。在这种情况下,您想要传递给方法的东西不是一个对象,而是一个简单的整数,因此您可以直接传递该整数。

Your method has a pointer as parameter:

-(void)addIconWithType:(UIIconType *)iconType;
// Takes a pointer to a UIIconType as argument

and you are passing it an integer:

[iconView addIconWithType:UIIconTypeStandard];
// UIIconTypeStandard is not a pointer to a UIIconType

Change your method definition to this:

-(void)addIconWithType:(UIIconType)iconType;

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 type MyClass. 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.

臻嫒无言 2024-11-14 15:55:40

更改

-(void)addIconWithType:(UIIconType *)iconType

为:

-(void)addIconWithType:(UIIconType)iconType

您的方法需要一个指向 UIIconType 的指针,但您试图按值传递它。

Change:

-(void)addIconWithType:(UIIconType *)iconType

to:

-(void)addIconWithType:(UIIconType)iconType

Your method expects a pointer to a UIIconType, but you are trying to pass it by value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文