如何在Objective-c中为C函数指定一个指向'self'(调用obj)的指针?

时间:2022-09-07 08:47:01

Hey all! I'm fairly new to objective-c so I have never had to tackle this issue yet. I have a C function in my objective-c class and I want to be able to call a method on a property of the containing objective-c class.

大家好!我对objective-c相当新,所以我从来没有解决过这个问题。我在objective-c类中有一个C函数,我希望能够在包含objective-c类的属性上调用一个方法。

I understand that my C function will not understand what 'self' is if I try to just send a message [self.delegate doSomething]

我明白,如果我尝试发送消息,我的C函数将无法理解“自我”是什么[self.delegate doSomething]

I assume what I need to do is give my c function a pointer to my class so I can do something like this:

我假设我需要做的是给我的c函数指向我的类,所以我可以做这样的事情:

myClass->delegate ->doSomething();

I would like to store a pointer to the current object (self) in a global variable because I am not able to change the function signatures. I want to be able to access the pointer from any C function defined within this class. The reason for this is that I am writing a wrapper around a C library I am trying to use. If it can be helped.. I'd rather not modify the source to this library.

我想在全局变量中存储指向当前对象(self)的指针,因为我无法更改函数签名。我希望能够从该类中定义的任何C函数访问指针。这样做的原因是我正在编写一个我正在尝试使用的C库的包装器。如果它可以帮助..我宁愿不修改此库的源。

Can someone please help me out with how to get a pointer to the current object? Thanks!

有人可以帮我解决如何获取指向当前对象的指针吗?谢谢!

 void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
    //[self.delegate privateMessage]; I would like to do the following
}

2 个解决方案

#1


17  

self is a pointer to the current method's object while inside of a method. So you can assign that to a global variable typed as your class:

self是在方法内部指向当前方法对象的指针。因此,您可以将其分配给键入的全局变量:

// Global scope
static MyClass *globalSelf;

// C function
void foo() {
    [globalSelf->delegate doSomething];
}

// ObjC method
- (void)setMyselfUpAsTheGlobalVariable {
    globalSelf = self;
    foo();
}

While this should work, I should point out that this is rather ugly and you have to be careful that the global variable isn't going to get trampled by concurrent code. Most C-style APIs will let you pass a void* variable as a user-defined parameter. If this is true with the API that you're wrapping, this would be the ideal place to store the Objective-C object instance rather than a global variable.

虽然这应该工作,但我应该指出这是相当丑陋的,你必须要小心,全局变量不会被并发代码践踏。大多数C风格的API都允许您将void *变量作为用户定义的参数传递。如果您正在包装的API也是如此,那么这将是存储Objective-C对象实例而不是全局变量的理想位置。

#2


0  

Declare it as below

声明如下

MyObjectiveCClass self;

Use the self extern variable to call obj-C function

使用self extern变量来调用obj-C函数

 void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
    [self.delegate privateMessage]; //I would like to do the following
}

Also check the SO post,

还要检查SO帖子,

Unable to call an Objective C method from a C function

无法从C函数调用Objective C方法

#1


17  

self is a pointer to the current method's object while inside of a method. So you can assign that to a global variable typed as your class:

self是在方法内部指向当前方法对象的指针。因此,您可以将其分配给键入的全局变量:

// Global scope
static MyClass *globalSelf;

// C function
void foo() {
    [globalSelf->delegate doSomething];
}

// ObjC method
- (void)setMyselfUpAsTheGlobalVariable {
    globalSelf = self;
    foo();
}

While this should work, I should point out that this is rather ugly and you have to be careful that the global variable isn't going to get trampled by concurrent code. Most C-style APIs will let you pass a void* variable as a user-defined parameter. If this is true with the API that you're wrapping, this would be the ideal place to store the Objective-C object instance rather than a global variable.

虽然这应该工作,但我应该指出这是相当丑陋的,你必须要小心,全局变量不会被并发代码践踏。大多数C风格的API都允许您将void *变量作为用户定义的参数传递。如果您正在包装的API也是如此,那么这将是存储Objective-C对象实例而不是全局变量的理想位置。

#2


0  

Declare it as below

声明如下

MyObjectiveCClass self;

Use the self extern variable to call obj-C function

使用self extern变量来调用obj-C函数

 void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
    [self.delegate privateMessage]; //I would like to do the following
}

Also check the SO post,

还要检查SO帖子,

Unable to call an Objective C method from a C function

无法从C函数调用Objective C方法