Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PyObjC中使用python函数回调?_Python_Objective C_Cocoa_Segmentation Fault_Pyobjc - Fatal编程技术网

在PyObjC中使用python函数回调?

在PyObjC中使用python函数回调?,python,objective-c,cocoa,segmentation-fault,pyobjc,Python,Objective C,Cocoa,Segmentation Fault,Pyobjc,我试图使用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. // #i

我试图使用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中的调用方法显然不存在。那么我如何让它存在于Objective-C中呢

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


谢谢你的帮助。

我想我会很高兴的

您不会保留传递到addPyFunc的create_callback()的结果_


因此,它可能会在你呼叫它之前被垃圾收集掉。

非常感谢。行得通D在调用之前删除它的位置和原因?instance.addPyFunc_u(create_callback(square[5])的执行方式如下:。计算平方(增加其参考计数)。计算[5](创建临时对象,refcount=1)。将这些参数传递给create_callback,并返回一个对象。[5]的递减计数。平方的递减计数。将结果对象传递给addPyFunc。减少对象的计数。它现在是零,所以垃圾收集它。注意,它是Python垃圾收集,而不是Cocoa。
#!/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()