在 PyObjC 中使用 python 函数回调?

发布于 2024-09-08 09:24:58 字数 1937 浏览 6 评论 0原文

我正在尝试使用 Python 中的 Objective-C 类来执行此操作,但 Objective-C 无法调用调用 python 函数的方法。

这是 Objective-C 代码的框架代码:

//
//  scalelib.h
//  Scalelib Cocoa Framework
//
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface Game : NSObject {
    id current_pyfunc;
}
-(void) addPyFunc: (id) pyfunc;
-(void) callPyFunc;
@end

//
//  scalelib.m
//  Scalelib Cocoa Framework
//
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Game.h"


@implementation Game
-(void) addPyFunc: (id) pyfunc{
    current_pyfunc = pyfunc;
}
-(void) callPyFunc{
    [current_pyfunc call]; //Segmentation fault. Method doesn't exist for some reason.
}
@end

这是加载框架并测试回调使用失败的 python 脚本。

#!/usr/bin/env python2.3
from objc import *
import os,sys
loadBundle("Scalelib Cocoa Framework",globals(),os.path.dirname(sys.argv[0]) + "/Scalelib Cocoa Framework/build/Release/Scalelib Cocoa Framework.framework/")
class PythonCallback(NSObject):
    def setCallback_withArgs_(self, python_function,args): #Python initialisation of class, add the callback function and arguments
        self.python_function = python_function
        self.args = args
        return self
    def call(self): #Used by Objective-C to call python function
        self.python_function(*self.args)
def create_callback(function,args):
    return PythonCallback.alloc().init().setCallback_withArgs_(function,args)
def square(num):
    print num**2
instance = Game.alloc().init()
callback = create_callback(square,[3])
callback.call()
instance.addPyFunc_(create_callback(square,[5]))
instance.callPyFunc()

我得到的输出:

9 分段错误

分段错误是因为Python中的调用方法显然不存在。那么如何让它存在于 Objective-C 中呢?

即使代码确实有效,它也是无用的,但我目前只是在测试一些东西。一旦回调工作正常,我就可以为 python 创建库了。

感谢您的帮助。

I'm trying to use an Objective-C class made in Python to do this but Objective-C can't call the method which calls the python function.

Here's the framework code which the Objective-C code:

//
//  scalelib.h
//  Scalelib Cocoa Framework
//
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface Game : NSObject {
    id current_pyfunc;
}
-(void) addPyFunc: (id) pyfunc;
-(void) callPyFunc;
@end

//
//  scalelib.m
//  Scalelib Cocoa Framework
//
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Game.h"


@implementation Game
-(void) addPyFunc: (id) pyfunc{
    current_pyfunc = pyfunc;
}
-(void) callPyFunc{
    [current_pyfunc call]; //Segmentation fault. Method doesn't exist for some reason.
}
@end

Here is the python script which loads the framework and tests the use of callbacks with failure.

#!/usr/bin/env python2.3
from objc import *
import os,sys
loadBundle("Scalelib Cocoa Framework",globals(),os.path.dirname(sys.argv[0]) + "/Scalelib Cocoa Framework/build/Release/Scalelib Cocoa Framework.framework/")
class PythonCallback(NSObject):
    def setCallback_withArgs_(self, python_function,args): #Python initialisation of class, add the callback function and arguments
        self.python_function = python_function
        self.args = args
        return self
    def call(self): #Used by Objective-C to call python function
        self.python_function(*self.args)
def create_callback(function,args):
    return PythonCallback.alloc().init().setCallback_withArgs_(function,args)
def square(num):
    print num**2
instance = Game.alloc().init()
callback = create_callback(square,[3])
callback.call()
instance.addPyFunc_(create_callback(square,[5]))
instance.callPyFunc()

I get the output:

9
Segmentation fault

The segmentation fault is because the call method made in python doesn't exist apparently. So how to I make it exist for Objective-C?

Even if the code did work it would be useless but I'm only testing things at the moment. Once I have the callbacks working, I'll be able to make my library for python.

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-09-15 09:24:58

我的猜测是保留计数。

您不会保留传递给 addPyFunc_ 的 create_callback() 的结果,

因此,在调用它之前它可能会被垃圾收集掉。

My guess would be retain counts.

You don't retain the result of create_callback() that you pass into the addPyFunc_

As such, it probably gets garbage collected away before you call it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文