纯C函数调用Objective-C方法? [英] Pure C function calling Objective-C method?

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

问题描述

好吧,我已经阅读了半数有关这个主题的主题,但没有一个解决方案看起来确实需要。



问题:



Pure C(.c)函数如何调用Pure Objective-C(.m)类中的方法?

每个示例/答案都在Objective-C(.m)方法中使用C.我有一个纯C库,我必须创建一个模拟器,所以我需要将我的内核保存在纯C中,并在Objective-C中调用我的更高级别的仿真方法。

我为创建引用和调用方法所做的任何尝试都失败。方括号表示法也失败。在Obj-C中创建全局变量并尝试在Pure-C中使用该变量不起作用,就好像命名空间是分离的。



任何人都这样做了吗?

以下是流程图:

Obj-C UIButton CLICKED->调用Obj-C方法 - >调用C函数 - >调用Obj-C方法



我的原始C文件已重命名为使用.m后缀。然后我添加了

  @interface myCLibrary:NSObject 

@end

添加到我的.h文件中,并添加到我以前的.c文件中,现在重新命名为.m。

  @implementation myCLibrary 

@end

请记住,C函数不会被粘贴在这些接口/实现声明之间,在它们下面。只有Objective-C才能进入这些语句。一旦我这样做,调用C函数,并调用返回到其他C函数运行良好。



感谢所有帮助。

Okay, I've read half a dozen threads on this subject, but none of the solutions appear to address exact needs.

Question:

How does a Pure C (.c) function call a method inside a Pure Objective-C (.m) class?

Every example / answer is using C inside an Objective-C (.m) method. I have a Pure C library that I have to create a simulator for, so I need to keep my Kernel in pure C and call out to my higher level emulation methods in Objective-C.

Any attempt I make to create a reference and call a method fails. Square bracket notation fails as well. Creating a global var in Obj-C and trying to use that in Pure-C doesn't work, as if the namespace is segregated.

Anyone done this?

Here's a diagram of the flow:

Obj-C UIButton CLICKED->Calls Obj-C method->Calls C function->Call Obj-C method

解决方案

After much experimenting, I found that the most elegant way of solving my problem was to turn my core C library into an NSObject using the .m suffix. The method of calling back and forth resolved instantly. This change DOES alter my original library, but by so little, it's manageable. So to review:

My original C file was renamed to use the .m suffix. Then I added

@interface myCLibrary : NSObject

@end

to my .h file, and added to my formerly .c file, now renamed .m.

@implementation myCLibrary

@end

Just remember that C functions aren't to be pasted between these interface / implementation declarations, below them. Only Objective-C is to go inside these statements. Once I did that, calling the C functions, and calling BACK to other C functions worked great.

Thanks for all the help regardless.

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

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