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

[英]How to use Objective-C in C++ header?


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();
  };
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/28/251c451f93793a4382d3b890d23ed22b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com