本文介绍了从C ++函数访问Objective-C变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个只需要重写的项目,但这不是一个选项。

I'm working on a project that just needs to be rewritten but that is not an option at this point.

我有一个C ++函数被调用,做各种东西。我需要它从App Delegate类中读取一个变量。

I have a C++ function that is called and does all kinds of stuff. I need it to read a variable from the App Delegate class.

例如我有:

@interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    MyViewController *viewController;

 int mToleranceLevel;
}



我有一个函数需要访问mToleranceLevel:

I then have a function that needs to access the mToleranceLevel:

bool FindExtrinsics(...)
{
 float maxError = mainDelegate.mMaxError;
        ...
}

问题是, :

@interface MyClass : UIViewController
{
  ...
}

@properties ...

bool FindExtrinsics(...);

@end

那么如何从AppDelegate类中获取一个值。我知道如何获得当前的委托:

So how would I get a value from the AppDelegate class. I do know how to get the current delegate:

mainDelegate = (RedStripeARAppDelegate *)[[UIApplication sharedApplication] delegate];

但是如何使用这个信息来获取我的C ++函数中的值。是否有一种方法来创建一个静态变量,所以我可以调用MyAppDelegate.mToleranceValue;

But how do I use this info to get the value in my C++ function. Is there a way to make a static variable so I can call MyAppDelegate.mToleranceValue;??

推荐答案

Xcode支持 Objective-C ++ ,它允许您使用C ++代码的Objective-C调用。将C ++代码文件的扩展名从.cpp(或.cc)更改为 .mm ,您就可以从C ++代码中获取值,就像从Objective-C代码中获取值一样。

Xcode supports Objective-C++, which enables you to use Objective-C calls from C++ code. Change the extension of your C++ code file from .cpp (or .cc) to .mm and you'll be able to get the value from your C++ code just as you would from Objective-C code.

这篇关于从C ++函数访问Objective-C变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:52