如何在c++ header中使用Objective-C ?

时间:2023-05-12 17:45:55

I tried to do the following in the A.h file:

我试着在A中做下面的事情。h文件:

#include "Bar.hpp"

#import <Foundation/Foundation.h>

namespace foo 
{
  struct A : Bar::B
  {
    public:

    A() : Bar::B() {}

    id delegate;

    virtual void OnEvent(...);
  };
}

But I get zillion of errors like 'I dont know what NSString is'. How do I do it correctly?

但是我有无数的错误,比如“我不知道NSString是什么”。我怎么做得正确?

2 个解决方案

#1


5  

You're including it in a .cpp file? Rename it to .mm (that's the correct file extension for Objective-C++).

你把它包含在。cpp文件中?将它重命名为.mm(这是objective - c++的正确文件扩展名)。

#2


1  

If you would like to use one of your Objective C classes inside your "regular" C++ class (as opposed to Objective C++) you could use the trick described in this article, which boils down to including <objc/objc-runtime.h> instead of <Foundation/Foundation.h>, and using a wrapped struct objc_object in place of a "real" Objective C object.

如果您想在“常规”c++类(与目标c++相反)中使用一个目标C类,您可以使用本文中描述的技巧,它可以归结为包括 的< /基础基础。用一个包装好的struct objc_object代替“真正的”Objective C对象。

#ifdef __OBJC__
@class ABCWidget;
#else
typedef struct objc_object ABCWidget;
#endif

namespace abc
{
  class Widget
  {
    ABCWidget* wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}

#1


5  

You're including it in a .cpp file? Rename it to .mm (that's the correct file extension for Objective-C++).

你把它包含在。cpp文件中?将它重命名为.mm(这是objective - c++的正确文件扩展名)。

#2


1  

If you would like to use one of your Objective C classes inside your "regular" C++ class (as opposed to Objective C++) you could use the trick described in this article, which boils down to including <objc/objc-runtime.h> instead of <Foundation/Foundation.h>, and using a wrapped struct objc_object in place of a "real" Objective C object.

如果您想在“常规”c++类(与目标c++相反)中使用一个目标C类,您可以使用本文中描述的技巧,它可以归结为包括 的< /基础基础。用一个包装好的struct objc_object代替“真正的”Objective C对象。

#ifdef __OBJC__
@class ABCWidget;
#else
typedef struct objc_object ABCWidget;
#endif

namespace abc
{
  class Widget
  {
    ABCWidget* wrapped;
  public:
    Widget();
    ~Widget();
    void Reticulate();
  };
}