如何从Objective-C调用C函数

时间:2021-12-26 19:57:16

I'm trying to figure out how to call a c function from an obj-c file. I've seen some good examples of how to do the opposite.

我试图弄清楚如何从obj-c文件中调用c函数。我已经看到了一些如何做相反的好例子。

In the example below I have an objective c file. It contains a c function named setup. I want to be able to create an instance of my obj-c file in the regular way and then call the setup function.

在下面的示例中,我有一个目标c文件。它包含一个名为setup的c函数。我希望能够以常规方式创建我的obj-c文件的实例,然后调用setup函数。

Header

#import <Foundation/Foundation.h>

void setup(int,float);

@interface Test : NSObject {
}

@end

Source

#import "Test.h"
#include <stdio.h>
#include <stdlib.h>

void setup(int val1,float val2)
{
    //do something with values  
}

@implementation Test

@end

View did load

Test *test =[Test alloc]init]

//this does not work
test.setup(6,1.4);

1 个解决方案

#1


7  

Just call setup(). As declared, is in no way tied to an object - it's just a regular C function.

只需调用setup()即可。正如声明的那样,它绝不会与对象相关联 - 它只是一个常规的C函数。

#1


7  

Just call setup(). As declared, is in no way tied to an object - it's just a regular C function.

只需调用setup()即可。正如声明的那样,它绝不会与对象相关联 - 它只是一个常规的C函数。