私は憤慨してobjcでプログラミングを始めましたが、既存のobjcアプリケーションでpythonコードをどのように使用できるかをさまよっていました。このサイトと他のサイトでいくつかのスレッドを見つけましたが、まったく役に立ちませんでした。私がやりたいことは、Apple の開発者 Web サイトにある SpotlightAPI を取得し、その結果を使用して Python コードで処理することです。
1. Apple から SpotlightAPI をダウンロード2. PyObjc
から cocoa-python アプリケーション テンプレートに基づいて新しいプロジェクトを作成
3. SpotlightAPI から PyObjC にコントローラー ファイルを追加
4. バインディングを構成SpotlightAPI の場合と同じ方法で
5. プログラムを実行すると、すべてが正常に動作します
6. NSObject (python.py と呼ばれる) から継承する新しい python クラス ファイルを作成しました
。 7. python ファイルには次のコードが含まれています。
from Foundation import *
import objc
NSObject = objc.lookUpClass(u"NSObject")
print "PYTHON VERSION BEING USED:"
class Python(NSObject):
def sendit_(self, something):
print something
return "asdf"
8. Controller.m ファイルの先頭に次のコードを挿入しました。
@class Python;
@interface NSObject (MethodsThatReallyDoExist)
-(NSString *) sendit: (id) aString;
@end
@implementation Controller
- (id)init
{
self = [super init];
Class pythonClass = NSClassFromString(@"Python");
myPython = [pythonClass new];
NSLog(@"Created PythonClass: %@", myPython);
NSString * oneString = [myPython sendit:@"asdf"];
NSLog(@"%@", oneString);
return self;
}
9. id *myPython;
Controller.h ファイルの変数宣言に
追加
10. 次のように controller.m ファイルに sendit メソッドを実装:
- (NSString *) sendit:(id) aString
{
return [myPython sendit:aString];
}
コンパイルして正常に実行されますが、init 関数から得られるコンソール メッセージはCreated PythonClass: (null)
次のとおり(null)
です。