如何在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