如何在Objective-c中为C函数指定一个指向'self'(调用obj)的指针? [英] How do I give C function a pointer to 'self' (calling obj) in objective-c?

查看:279
本文介绍了如何在Objective-c中为C函数指定一个指向'self'(调用obj)的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我认为我需要做的就是给我c函数指向我的类,所以我可以这样做:



myClass-> delegate - > doSomething();



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



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

  void event_privmsg(irc_session_t * session,const char * event,const char * origin,const char ** params,unsigned int count )
{
// [self.delegate privateMessage];我想做以下
}


解决方案

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

  //全局范围
static MyClass * globalSelf ;

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

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

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


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.

I understand that my C function will not understand what 'self' is if I try to just send a message [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:

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.

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
}

解决方案

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:

// 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.

这篇关于如何在Objective-c中为C函数指定一个指向'self'(调用obj)的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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