在PyObjC中使用python函数回调?

2024-10-04 07:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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脚本,它加载框架并在失败的情况下测试回调的使用。在

^{pr2}$

我得到输出:

九 分段故障

分段错误是因为python中的call方法显然不存在。那么我如何使它存在于Objective-C中呢?在

即使代码真的起作用了也没用,但我现在只是在测试。一旦回调生效,我就可以为python创建库了。在

谢谢你的帮助。在


Tags: 方法代码框架idgamebyframeworkcurrent