0

我有一个问题,伙计们。我需要从 C++ 函数为我的 iPad 应用程序中的 UITextView 控制器分配值。因此,C++ 函数返回一个字符串,我可以在输出窗口中看到该输出。我使用 .mm 文件合并了 C++ 和 XCode 目标 C。现在,我需要从 C++ 函数中获取值并将它们添加到 UITextFiled 中。

比如说。我的 C++ 函数如下所示: .cpp 文件

void *consumer (void* data)
{
SyncBuffThang<GLOBAL_BUFF_LEN,GLOBAL_BUFFS>* cc =(SyncBuffThang<GLOBAL_BUFF_LEN,GLOBAL_BUFFS>*) data;

affinity("consumer", cons);

for (int ii=0; ii<100; ii++)
{
    unsigned char c = cc->get();
    cc->res = c;
    myVar = c;
    cerr << "Consumer Get" << myVar << endl;
    f +=c;
}

cerr << "Leaving consumer in method cons" << f  << endl;

return 0;
}


int PC9::RunPC()
{
SyncBuffThang<GLOBAL_BUFF_LEN, GLOBAL_BUFFS> pc;

pthread_t p, c;

pthread_create(&p, 0, producer, &pc);

pthread_create(&c, 0, consumer, &pc);

pthread_join(p, 0);

pthread_join(c,0);
}

以下播种 .mm 文件代码。

#import "PC.h"
#import "PC9.h"
#import "GV.h"

@implementation PC

-(void)callFunctionPC
 {
      PC9   * myCPlusPlusObj; //A C++ object
      myCPlusPlusObj=new PC9();
      myCPlusPlusObj-> RunPC();
  }

 @end

在上面的函数中看到我可以打印 myVar 但是,我不知道如何从我的 iPad 应用程序访问它或在 UITextFile 上查看它。

朋友们,我真的很感谢你的帮助。

提前致谢。

-T

4

1 回答 1

1

这是我一直在等待的答案!!!

你好世界.h

  #ifndef __Demo1__HelloWorld__
  #define __Demo1__HelloWorld__
  #include <iostream>
  #endif 

  class HelloWorld {

  public:
      std::string Mtd_HelloWorld();
  };

你好世界.cpp

   #include "HelloWorld.h"
   std::string HelloWorld::Mtd_HelloWorld()
    {
       std::string output;
       output = "This is from C++";
       return output;
     }

你好世界M.h

   #import <Foundation/Foundation.h>

   @interface HelloWorld_M : NSObject
   -(NSString *)CallCPP;
   -(UIView *)CreateTextView:(NSString *)input;
   @end

你好世界.mm

    #import "PCViewController.h"
    #include "HelloWorld.h"
    #include "HelloWorld_M.h"
    @implementation HelloWorld_M
   -(NSString *)CallCPP
     {
      HelloWorld    * myCPlusPlusObj; //A C++ object
      myCPlusPlusObj=new HelloWorld();
      std::string res = myCPlusPlusObj-> Mtd_HelloWorld();
      NSString *result = [NSString stringWithCString:res.c_str()
                                            encoding:[NSString defaultCStringEncoding]];
      return result;
      }

     -(UIView *)CreateTextView:(NSString *)input
      {
      UITextView *myTextView = [[UITextView alloc] initWithFrame: CGRectMake (0.0,0.0,320.0,200.0)];
         myTextView.text = input;
         return myTextView;
       }

      @end

PCViewController.h

     #import <UIKit/UIKit.h>
     #import "HelloWorld_M.h"

     @interface PCViewController : UIViewController
      {
        HelloWorld_M *objHelloWorld;
       }
    - (IBAction)RunHelloWorld:(id)sender;
     @end

PCViewController.m

   #import "PCViewController.h"

   @interface PCViewController ()

   @end

   @implementation PCViewController

  - (void)viewDidLoad
   {
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    }

  - (void)didReceiveMemoryWarning
   {
   [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
    }

    - (IBAction)RunHelloWorld:(id)sender
     {
      objHelloWorld = [[HelloWorld_M alloc]init];
      [self.view addSubview: [objHelloWorld CreateTextView:[objHelloWorld CallCPP]]] ;
       }
  @end
于 2013-05-29T22:52:30.413 回答