PyObjC and user blocks

The official documentation says that you can use custom blocks in python code, but you need to create metadata. I have not found an example of this.

My question is how to create, use and distribute metadata for custom blocks.

Example

@interface SomeClass

- (void)doSomethingWithCompletion: (void (^)(SomeObject *obj, NSError *error))myBlock;

@end


def pythonMethod():
    def completion(obj, error):
        # staff
    foo = SomeClass.new()
    foo.doSomethingWithCompletion_(somehow_pass_completion)

The question is how it should look somehow_pass_completionand how to provide metadata for myBlock.

+5
source share
1 answer

- , XML, Objective-C. , PyObjC , Python, Objective-C. , ; .bridgesupport PyObjC. AppKit, , /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport. Objective-C Apple gen_bridge_metadata . Theres man 5 BridgeSupport . 1

PyObjC objc.registerMetaDataForSelector objc.parseBridgeSupport, , Python dicts ( ), XML, BridgeSupport (), registerMetaData... pyobjc : pyobjc/pyobjc-core/PyObjCTest/test_metadata * ( test_metadata*.py ) 2.

, -[NSSavePanel beginWithCompletionHandler:], :

<method selector='beginWithCompletionHandler:'>
    <arg index='0' block='true' >
        <retval type='v' />
        <arg type='i' type64='q' />
    </arg>
</method>

arg - Type Encodings, @encode Obj-C. .

, , Objective-C, gen_bridge_metadata, .bridgesupport, , objc.parseBridgeSupport in. objc.registerMetaDataForSelector ; .

PyObjC, , :

def pythonMethod():
    def myCompletionHandler(obj, error):
        pass
foo = SomeClass.new()
foo.doSomethingWithCompletion_(myCompletionHandler)

, PyObjC: PyObjC | | openPanelDidEnd. .

, , , , - . , . , - - , , !


1 Apple, : Framework.
2 pyobjc-dev

+6

All Articles