Reputation: 1
i am having objective c class, which i have a C function in it. in that C function i want to call to ANOTHER c function in another class.
in the first class C function , i do this :
HelloWorldLayer *ran;
ran=[[HelloWorldLayer alloc] init];
[ran showDigital:BinaryWord];
when HelloWorldLayer
is the other class, which have the C function :
void showDigital( int word[4])
and it declared also in the HelloWorldLayer.h
.
in the first class, when i am trying to call the other function it says that showDigital function is not found.
is it has to do with the fact that its a C function ?
thanks.
Upvotes: 1
Views: 227
Reputation: 86651
First of all, the C function is not in the other class, it is merely in the same file as the other class. Objective-C classes have methods not functions.
So you can call your C function from anywhere provided you have an extern declaration of it. e.g. in 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. 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. You still won't be able to access private instance variables directly though.
void showDigital(id self, int* word)
{
[self foobar];
}
Upvotes: 1
Reputation: 34912
Do you want to call a C function or an Objective-C method? 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.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. You could define it in your @interface
block, but it wouldn't be part of that Objective-C class. Only Objective-C methods can be part of the class.
So for instance if you really really really wanted you could do:
HelloWorldLayer.h
@interface HelloWorldLayer : NSObject
void sendDigital(int[4] word);
@end
But when you called sendDigital
you'd have to do it like this:
int word[4] = {1,2,3,4};
sendDigital(word);
And you'll notice there's no HelloWorldLayer
around at all. 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.
Upvotes: 2