[英]Calling an Objective C function from a C function passing an array of floats
I´m trying to pass a reference of array of floats. 我试图传递浮点数组的引用。 The problem is the call, because I´am developing for c but I want to make a call to an Objective C Function, Could anyone help me? 问题是电话,因为我正在为c开发,但我想调用Objective C函数,任何人都可以帮助我吗? How can I make the call? 我怎么打电话? There you have the code: 你有代码:
bool VideoCamera_Camera(float *buffer) {
[VideoCameraBinded VideoCamera_CameraUpdateBinded: buffer];
}
Thank you 谢谢
I'm going to assume that VideoCameraBinded is an instance, and not a class. 我将假设VideoCameraBinded是一个实例,而不是一个类。 If I am mistaken, please let me know. 如果我弄错了,请告诉我。
If you have a method defined on VideoCameraBinded's class, something like this: 如果你有一个在VideoCameraBinded的类上定义的方法,这样的事情:
- (void)VideoCamera_CameraUpdateBinded:(float *)buffer {
//...
}
then I don't know where your problem is coming from. 那时我不知道你的问题来自哪里。 Are you getting a specific error or some other issue? 您是否收到特定错误或其他问题?
If you have access to and can change the Objective-C code, add a C API there. 如果您有权访问并可以更改Objective-C代码,请在其中添加C API。
Otherwise, if you really can't change the Objective-C code you can use the Objective-C runtime directly, but this is discouraged: 否则,如果您真的无法更改Objective-C代码,则可以直接使用Objective-C运行时,但不建议这样做:
#include <objc/runtime.h>
objc_msgSend(VideoCameraBinded, // receiver
sel_registerName("VideoCamera_CameraUpdateBinded:"), // selector
buffer); // comma separated list of arguments
You need to link to an Objective-C runtime library, usually libobjc: 您需要链接到Objective-C运行时库,通常是libobjc:
$ clang mycode.c -lobjc
$ # or cc if you use GCC
If the Objective-C method expects an NSArray *
instead of a float *
, you can use Core Foundation with CFArrayRef
. 如果Objective-C方法需要NSArray *
而不是float *
,则可以将Core Foundation与CFArrayRef
一起CFArrayRef
。 CFArrayRef
and NSArray *
are interchangeable, but CFArrayRef
is a C type so you can use that. CFArrayRef
和NSArray *
是可互换的,但CFArrayRef
是C类型,因此您可以使用它。 Same goes for CFNumberRef
and NSNumber *
, see Apple's documentation on this . CFNumberRef
和NSNumber *
,请参阅Apple关于此的文档 。
What is the parameter type for VideoCamera_CameraUpdateBinded:
? VideoCamera_CameraUpdateBinded:
的参数类型是什么VideoCamera_CameraUpdateBinded:
If its NSArray then you have to loop, create an array and send it like any other obj-c method. 如果它的NSArray然后你必须循环,创建一个数组并像任何其他obj-c方法一样发送它。 You'll have to store the floats in some kind of objects (eg NSNumber
). 您必须将浮动存储在某种对象中(例如NSNumber
)。
Otherwise, if the obj-c function is taking a float* then you should be good to go. 否则,如果obj-c函数正在浮动*那么你应该好好去。
BTW, shouldn't you pass a bufferSize parameter too? 顺便说一下,你不应该传递一个bufferSize参数吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.