7

I'm developing a Python/ObjC application and I need to call some methods in my Python classes from ObjC. I've tried several stuffs with no success.

  • How can I call a Python method from Objective-C?
  • My Python classes are being instantiated in Interface Builder. How can I call a method from that instance?

1 Answer 1

18

Use PyObjC.

It is included with Leopard & later.

>>> from Foundation import *
>>> a = NSArray.arrayWithObjects_("a", "b", "c", None)
>>> a
(
      a,
      b,
      c
)
>>> a[1]
'b'
>>> a.objectAtIndex_(1)
'b'
>>> type(a)
<objective-c class NSCFArray at 0x7fff708bc178>

It even works with iPython:

In [1]: from Foundation import *

In [2]: a = NSBundle.allFrameworks()

In [3]: ?a
Type:       NSCFArray
Base Class: <objective-c class NSCFArray at 0x1002adf40>

`

To call from Objective-C into Python, the easiest way is to:

  • declare an abstract superclass in Objective-C that contains the API you want to call

  • create stub implementations of the methods in the class's @implementation

  • subclass the class in Python and provide concrete implementations

  • create a factory method on the abstract superclass that creates concrete subclass instances

I.e.

@interface Abstract : NSObject
- (unsigned int) foo: (NSString *) aBar;
+ newConcrete;
@end

@implementation Abstract
- (unsigned int) foo: (NSString *) aBar { return 42; }
+ newConcrete { return [[NSClassFromString(@"MyConcrete") new] autorelease]; }
@end

.....

class Concrete(Abstract):
    def foo_(self, s): return s.length()

.....

x = [Abstract newFoo];
[x  foo: @"bar"];
Sign up to request clarification or add additional context in comments.

3 Comments

I do not see how this works. For example, I'm assuming the following: (1) put the python code in a file Concrete.py (2) put the obj-c code in Abstract.h/m Given that, how exactly do you build this program?
Update: There is clarification on this ticket: link
Is this missing a closing parenthesis after "MyConcrete"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.