objc 整数和 C++ 之间的转换Objective C 中的枚举代码

发布于 2024-12-18 15:29:40 字数 671 浏览 1 评论 0原文

我有一个返回整数的 Objective C 选择器。我有一个需要枚举的 C++ 实例方法。我怎样才能链接它们?当我这样做时,我正在上 Objective C++ (.mm) 课程。

我想这样调用:

TKClass::foo(MyEnum enumVal) { ... }

使用以下返回值:

- (int) intValue { ... }

像这样:

myCPPInstance->foo([myObjCInstance intValue]);

我尝试过强制转换 (foo((MyEnum) [myObjCInstance integerValue])) 但它不起作用。我绝对不希望我的 Objective C 对象知道有关枚举的任何信息; intValue 需要保持为整数。同样,我真的不想让 C++ 方法在应该接受枚举时担心整数输入。

我不是一个 C++ 程序员,如果这很简单,我很抱歉。

谢谢

编辑:枚举定义如下:

enum MyEnum { 
   Apples = 0,
   Bananas = 1,
   Chocolate = 2
};

I have an Objective C selector which returns integers. I have a C++ instance method which expects an enum. How can I link them? I'm in an Objective C++ (.mm) class while I'm doing this.

I want to call this:

TKClass::foo(MyEnum enumVal) { ... }

With the return value of this:

- (int) intValue { ... }

Like this:

myCPPInstance->foo([myObjCInstance intValue]);

I've tried casting (foo((MyEnum) [myObjCInstance integerValue])) but it doesn't work. I definitely don't want my Objective C object to know anything about the enum; intValue needs to stay as an integer. Similarly, I don't really want to have the C++ method worry about integer inputs when it should be accepting enums.

I'm not much of a C++ programmer, so sorry if this is easy.

Thanks

EDIT: The enum is defined like this:

enum MyEnum { 
   Apples = 0,
   Bananas = 1,
   Chocolate = 2
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

在巴黎塔顶看东京樱花 2024-12-25 15:29:41

这是一个命名空间问题。演员阵容应该是:

myCPPInstance->foo((myNamespace::MyEnum) [myObjCInstance intValue]);

It was a namespace issue. The cast should have been:

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