无法从C函数调用Objective C方法 [英] Unable to call an Objective C method from a C function

查看:81
本文介绍了无法从C函数调用Objective C方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVC架构用于GUI应用程序.该模型类具有一些C函数. C函数之一调用Objective-C类的某些方法.我使用该类的对象调用这些方法.发生的一件奇怪的事是,xyz方法之前的方法被完美地调用,但是当该xyz方法被调用时,该方法及其之后的方法没有得到执行. 我没有任何错误.因此无法弄清楚到底发生了什么. 关于这可能是什么原因的任何想法?

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?

推荐答案

正如Marc指出的那样,您可能正在使用对未在Objective-C范围之外初始化的OBJC对象的引用.

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

这是一个调用ObjC对象方法的C代码的工作示例:

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];
}

这篇关于无法从C函数调用Objective C方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆