objective-c – 我可以将:: bind()提升为Objective C函数吗?
发布时间:2020-12-16 05:51:18 所属栏目:百科 来源:网络整理
导读:我不知道这是否可行,但如果是这样,语法将如何? 如果不可能,为什么不呢? 解决方法 您应该能够绑定到消息实现(IMP),它们只是具有两个隐藏参数的简单C函数,分别为id和SEL类型的self和_cmd. 编辑:刚刚测试了以下完整的例子,它似乎工作. #include stdio.h#incl
我不知道这是否可行,但如果是这样,语法将如何?
如果不可能,为什么不呢? 解决方法
您应该能够绑定到消息实现(IMP),它们只是具有两个隐藏参数的简单C函数,分别为id和SEL类型的self和_cmd.
编辑:刚刚测试了以下完整的例子,它似乎工作. #include <stdio.h> #include <boost/bind.hpp> #include <Foundation/NSObject.h> @interface MyClass : NSObject { } -(int) doSomething:(int)arg; @end @implementation MyClass -(int) doSomething:(int)arg { printf("doSomething: self=0x%08x _cmd=0x%08xn",self,_cmd); return arg + 1; } @end int main(void) { MyClass *myObj = [[MyClass alloc] init],*otherObj = [[MyClass alloc] init]; typedef int (*MyFunc)(id,SEL,int); SEL doSomething_sel = @selector(doSomething:); MyFunc doSomething_impl = (MyFunc)[myObj methodForSelector:doSomething_sel]; // bind self & _cmd arguments: // calls [myObj doSomething:x] int result = boost::bind(doSomething_impl,myObj,doSomething_sel,_1)(14); printf("result1: %dn",result); // bind _cmd & arg: // calls [otherObj doSomething:3] result = boost::bind(doSomething_impl,_1,42)(otherObj); printf("result2: %dn",result); return 0; } 用GNUstep编译为: gcc objcbind.mm -o objcbind -I/usr/include/GNUstep -lobjc -lstdc++ -lgnustep-base 在Mac OS X上,编译为: gcc objcbind.mm -o objcbind -framework Foundation -lstdc++ 输出: doSomething: self=0x01a85f70 _cmd=0x00602220 result1: 15 doSomething: self=0x01a83d70 _cmd=0x00602220 result2: 43 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |