简体   繁体   English

在另一个类中调用ac函数

[英]calling a c function in another class

i am having objective c class, which i have a C function in it. 我有目标c类,我有一个C函数。 in that C function i want to call to ANOTHER c function in another class. 在那个C函数中,我想在另一个类中调用另一个AN函数。

in the first class C function , i do this : 在第一类C函数中,我这样做:

  HelloWorldLayer *ran;
  ran=[[HelloWorldLayer alloc] init];
  [ran showDigital:BinaryWord];

when HelloWorldLayer is the other class, which have the C function : HelloWorldLayer是另一个具有C函数的类时:

void showDigital( int word[4])

and it declared also in the HelloWorldLayer.h . 它也在HelloWorldLayer.h声明。

in the first class, when i am trying to call the other function it says that showDigital function is not found. 在第一堂课中,当我尝试调用其他函数时,它说找不到showDigital函数。

is it has to do with the fact that its a C function ? 它与C函数有关吗?

thanks. 谢谢。

Do you want to call a C function or an Objective-C method? 您想调用C函数还是Objective-C方法? You've got your terminology all muddled. 你的术语都混乱了。 I think you want to define an Objective-C method on HelloWorldLayer and call that, so here is what your header should look like: 我认为您想在HelloWorldLayer上定义一个Objective-C方法并调用它,因此您的标头应如下所示:

HelloWorldLayer.h HelloWorldLayer.h

@interface HelloWorldLayer : NSObject

- (void)sendDigital:(int[4])word;

@end

Then you can call that method by doing what you have already tried. 然后你可以通过做你已经尝试过的方法来调用该方法。

You can't declare a C function inside an Objective-C class. 您不能在Objective-C类中声明C函数。 You could define it in your @interface block, but it wouldn't be part of that Objective-C class. 您可以在@interface块中定义它,但它不属于那个Objective-C类。 Only Objective-C methods can be part of the class. 只有Objective-C方法可以成为该类的一部分。

So for instance if you really really really wanted you could do: 因此,举例来说,如果你真的真的 真的想你可以这样做:

HelloWorldLayer.h HelloWorldLayer.h

@interface HelloWorldLayer : NSObject

void sendDigital(int[4] word);

@end

But when you called sendDigital you'd have to do it like this: 但是,当您调用sendDigital您必须这样做:

int word[4] = {1,2,3,4};
sendDigital(word);

And you'll notice there's no HelloWorldLayer around at all. 你会注意到根本没有HelloWorldLayer Now do you understand the difference here? 现在您了解这里的区别了吗? I really do think you need to define your method as an Objective-C method rather than a C function. 我确实认为您需要将方法定义为Objective-C方法而不是C函数。

First of all, the C function is not in the other class, it is merely in the same file as the other class. 首先,C函数是不是其他类中,它仅仅是在同一个文件中的其他类。 Objective-C classes have methods not functions. Objective-C类的方法不是函数。

So you can call your C function from anywhere provided you have an extern declaration of it. 所以你可以从任何地方调用你的C函数,只要你有一个extern声明它。 eg in HelloWorldLayer.h 例如在HelloWorldLayer.h

extern void showDigital(int* word);

If you really want the function to be associated with instances of your class, you need to make it an Objective-C method. 如果您确实希望该函数与您的类的实例相关联,则需要将其设置为Objective-C方法。 You can have that method be a thin wrapper for the original function if you like: 如果您愿意,可以将该方法作为原始函数的瘦包装:

-(void) showDigital: (int*) word
{
    showDigital(word);
}

If you want to pretend that your C function is part of an object, you'll need to add the receiver object as a parameter. 如果您想假装您的C函数是对象的一部分,则需要将接收器对象添加为参数。 You still won't be able to access private instance variables directly though. 但您仍然无法直接访问私有实例变量。

void showDigital(id self, int* word)
{
    [self foobar];
}

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

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