代码之家  ›  专栏  ›  技术社区  ›  Gary

如何从C函数调用objective-C函数

  •  2
  • Gary  · 技术社区  · 14 年前

    我试图从C函数调用objective-C函数,但代码在objc\u msgSend中不断崩溃。

    void c_function(int arg0, const char *arg1)  
    {  
       [[objc_class instance] testFunction:arg0 Arg1:arg1];  
    }
    

    gdb显示在调用objective-C函数时发生崩溃。如何从C函数中调用objective-C函数?

    谢谢

    3 回复  |  直到 14 年前
        1
  •  2
  •   Jared Pochtar    14 年前

    您的代码没有问题,因为从c函数调用objc方法没有特殊的规则。如果objc\u msgSend发生崩溃,请参阅 http://www.sealiesoftware.com/blog/archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html . 如果objc行在其他objc代码中,同样的情况也会发生——很可能您忘记了保留singleton的共享实例。

        2
  •  -1
  •   Objective Interested Person    14 年前

    [[objc_class instance] testFunction:arg0 Arg1:arg1];

    由于您没有提供更多信息:

    对象类 是一个例子,你的单身汉?它处理信息 实例

    实际上是一个类,有一个类方法 实例 ? 这会给你另一个例子

    换句话说,你真的有

    @interface class2 { } -(void) testFunction:(int)count Arg1:(const char *)args; @end

    @include "class2.h" @interface objc_class { } -(class2*) instance { } -(void) testFunction:(int)count Arg1:(const char *)args; @end

    @include "class2.h" @interface objc_class { } +(class2*) instance; // be aware of the fact, instance (normaly) does not have // access to any instance variables!

    包括在内?

    (你确定你理解目标c吗?)

        3
  •  -1
  •   Justin user327094    14 年前

    @interface class2 {
    
    }
    
    -(void) testFunction:(int)count Arg1:(const char *)args;
    
    @end
    

    @include "class2.h"
    
    @interface objc_class {
    
    }
    
    -(class2*) instance;
    
    @end 
    

    三:

    @include "class2.h"
    
    @interface objc_class {
    
    }
    
    +(class2*) instance; // be aware of the fact, instance (normally) does not
                         // have access to any instance variables!