简体   繁体   English

libobjc:从普通 C 调用 ObjC 方法?

[英]libobjc: Calling a ObjC method from plain C?

I am currently experimenting with the V programming language and looked at a few fun things I wanted to try out.我目前正在试验V 编程语言,并查看了一些我想尝试的有趣的东西。 One of the things that I wondered was, if I could call Objective-C methods from plain C without compiling a single .m file.我想知道的一件事是,是否可以在不编译单个.m文件的情况下从纯 C 调用 Objective-C 方法。

Since Python can do it with pyobjc, I wonder if I could make it work in V?既然Python可以用pyobjc来做,不知道能不能用V来做?

Imagine the following, very crude code:想象一下以下非常粗糙的代码:

@implementation Foo

- (id) greet: (char*)me {
  printf("Hello %s!", me);
}

@end

Now, I know that with objc_sendMsg I can send messages - but how do I create the proper instance first?现在,我知道使用objc_sendMsg我可以发送消息 - 但我如何首先创建正确的实例?

It's going to look something like this:它看起来像这样:

Class fooClass = objc_lookUpClass("Foo");
id obj = class_createInstance(fooClass, 0);
SEL initSelector = sel_registerName("init");
((id (*)(id, SEL))objc_msgSend)(obj,initSelector);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM