如何将corebluetooth从Objective C移植到Objective C ++ [英] How to port corebluetooth from Objective C to Objective C++

查看:136
本文介绍了如何将corebluetooth从Objective C移植到Objective C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我正在从事的项目中获得一些帮助.我目前正在尝试重新编写此代码https://github.com/djwait/用Objective-c编写的CoreBluetoothExample 到Openframeworks ios Objective c ++中.但是,我是Objective -c的完整入门者,对如何进行感到迷茫.目的是从原始应用程序访问变量uint16_t heartRate并将其在c ++代码中使用.对于原始代码如何创建ACViewController对象以及如何调用其成员函数,我尤其感到困惑. 我当前的方法是将原始ACViewController .h复制到我的新项目中.然后将ACViewController.h导入我的ofApp.h中,创建一个类: ACViewController * acv;

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++. However I am a complete beginner to Objective -c and am feeling lost as to how to proceed. The aim is to get access to the variable uint16_t heartRate (from the original app) and use it in c++ code. 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. My current method has been to copy over the original ACViewController .h to my new project. Then import the ACViewController.h in my ofApp.h, Create a class: ACViewController *acv;

然后在设置中:acv = [[ACViewController alloc] init]; 但是每当我尝试调用诸如[acv renderHeartRateMeasurement]之类的函数时,我明白了 找不到实例方法"-renderHeartRateMeasurement"(返回类型默认为"id")

Then in setup: acv = [[ACViewController alloc]init]; But whenever I try to call a function such as [acv renderHeartRateMeasurement]; i get Instance method '-renderHeartRateMeasurement' not found (return type defaults to 'id')

任何帮助将不胜感激!

最好, 古斯塔夫

推荐答案

如果我对您的理解正确,则希望能够从C ++调用Objective-C方法.

If I understand you correctly, you want to be able to call Objective-C methods from C++.

假设您在MyClass.h中定义了一个Objective-C类,如下所示:

Let's assume you have an Objective-C class defined in MyClass.h as follows:

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

一个非常简单的类,它的初始化需要两个int.

A very simple class, with an init that takes two ints.

要从C ++访问它,请在MyCppClass.hpp中执行以下操作:

To access it from C++, you would do the following in your 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();
};
}

如果您的C ++代码将调用ObjC类/方法,则ObjC运行时必须可用.宏允许在C ++代码中对ObjC类进行前向声明,并且它将编译并正常运行. C ++不会抱怨.只需在您的C ++文件中包含MyCppClass.hpp,然后像使用任何C ++类一样使用它即可.

If your C++ code will call ObjC classes/methods then the ObjC runtime has to be available. The macros allow forward declaration of ObjC classes in C++ code, and it will compile and run just fine. C++ will not complain. Just include MyCppClass.hpp in your C++ file, and use it like any C++ class.

现在,最后一步是在MyCppClass.mm中,这是一个Objective-C ++文件,定义如下:

Now the last step is in your MyCppClass.mm, which is an Objective-C++ file define as follows:

#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];
}

最后一点,MyCppClass.mm需要使用-fno-objc-arc标志进行编译

One final note, MyCppClass.mm needs to be compiled with compile with -fno-objc-arc flag

这篇关于如何将corebluetooth从Objective C移植到Objective C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆