Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to call an Objective C method from a C function

Tags:

objective-c

I am using MVC architecture for a GUI application. The model class has some C functions. One of the C functions calls some methods of Objective-C class. I call those methods using an object of that class. The strange thing happening is that methods previously to the an xyz method are called perfectly but when that xyz method is called, that method and the methods after it aren't getting executed. I don't get any errors. So can't figure out what exactly is happening. Any ideas as to what might be the reason for this?

like image 996
shrads Avatar asked Nov 20 '08 10:11

shrads


1 Answers

As Marc points out, you're probably using a reference to the OBJC object that is un-initialised outside the objective-c scope.

Here's a working sample of C code calling an ObjC object's method:

#import <Cocoa/Cocoa.h>

id refToSelf;

@interface SomeClass: NSObject
@end

@implementation SomeClass
- (void) doNothing
{
        NSLog(@"Doing nothing");
}
@end

int otherCfunction()
{
        [refToSelf doNothing];
}


int main()
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    SomeClass * t = [[SomeClass alloc] init];
    refToSelf = t;

    otherCfunction();

    [pool release];
}
like image 113
diciu Avatar answered Sep 21 '22 06:09

diciu