简体   繁体   English

如何将corebluetooth从Objective C移植到Objective C ++

[英]How to port corebluetooth from Objective C to Objective C++

I was hoping to get some help with a project I'm working on. 我希望在我正在从事的项目中获得一些帮助。 I'm currently trying to rework this code https://github.com/djwait/CoreBluetoothExample written in Objective -c into Openframeworks ios Objective c++. 我目前正在尝试将用Objective-c编写的代码https://github.com/djwait/CoreBluetoothExample改写为Openframeworks ios Objective c ++。 However I am a complete beginner to Objective -c and am feeling lost as to how to proceed. 但是,我是Objective -c的完整入门者,对如何进行感到迷茫。 The aim is to get access to the variable uint16_t heartRate (from the original app) and use it in c++ code. 目的是从原始应用程序访问变量uint16_t heartRate并将其用于c ++代码。 More than anything I'm confused as to how the original code is creating an ACViewController object and as to how its member functions are being called. 对于原始代码如何创建ACViewController对象以及如何调用其成员函数,我感到非常困惑。 My current method has been to copy over the original ACViewController .h to my new project. 我当前的方法是将原始ACViewController .h复制到我的新项目中。 Then import the ACViewController.h in my ofApp.h, Create a class: ACViewController *acv; 然后将ACViewController.h导入我的ofApp.h中,创建一个类:ACViewController * acv;

Then in setup: acv = [[ACViewController alloc]init]; 然后在设置中:acv = [[ACViewController alloc] init]; But whenever I try to call a function such as [acv renderHeartRateMeasurement]; 但是每当我尝试调用[acv renderHeartRateMeasurement]之类的函数时, i get Instance method '-renderHeartRateMeasurement' not found (return type defaults to 'id') 我得到找不到实例方法“ -renderHeartRateMeasurement”(返回类型默认为“ id”)

Any help would be much appreciated! 任何帮助将非常感激!

Best, Gustaf 最好,古斯塔夫

If I understand you correctly, you want to be able to call Objective-C methods from C++. 如果我对您的理解正确,则希望能够从C ++调用Objective-C方法。

Let's assume you have an Objective-C class defined in MyClass.h as follows: 假设您在MyClass.h中定义了一个Objective-C类,如下所示:

@interface MyClass : NSObject
-(instancetype)initWithA:(int)a andB:(int)b;
@end

A very simple class, with an init that takes two ints. 这是一个非常简单的类,其初始化需要两个整数。

To access it from C++, you would do the following in your MyCppClass.hpp: 要从C ++访问它,您将在MyCppClass.hpp中执行以下操作:

#include <objc/objc-runtime.h>
#ifdef __OBJC__
#define OBJC_CLASS(name) @class name // if imported in ObjC it knows it is a class
#else
#define OBJC_CLASS(name) typedef struct objc_object name // C++ will treat it as a struct, which is what an ObjC class is internally
#endif

OBJC_CLASS(MyClass);

namespace MyWrapper {
class MyCppClass {
    MyClass* myObj;
public:
    MyCppClass(int a, int b);
    ~MyCppClass();
};
}

If your C++ code will call ObjC classes/methods then the ObjC runtime has to be available. 如果您的C ++代码将调用ObjC类/方法,则ObjC运行时必须可用。 The macros allow forward declaration of ObjC classes in C++ code, and it will compile and run just fine. 宏允许在C ++代码中对ObjC类进行前向声明,并且它将编译并正常运行。 C++ will not complain. C ++不会抱怨。 Just include MyCppClass.hpp in your C++ file, and use it like any C++ class. 只需在您的C ++文件中包含MyCppClass.hpp,然后像使用任何C ++类一样使用它即可。

Now the last step is in your MyCppClass.mm, which is an Objective-C++ file define as follows: 现在,最后一步是在MyCppClass.mm中,这是一个Objective-C ++文件,定义如下:

#include "MyCppClass.hpp"
#import "MyClass.h" // this is your ObjC class, imported here because you can't import it in C++ header

namespace MyWrapper {
MyCppClass::MyCppClass(int a, int b) :
myObj([[MyClass alloc] initWithA:a andB:b]) { }

MyCppClass::~ MyCppClass() {
    [myObj release];
}

One final note, MyCppClass.mm needs to be compiled with compile with -fno-objc-arc flag 最后一点,MyCppClass.mm需要使用-fno-objc-arc标志进行编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM