Back to snippets

pyobjc_python_objc_bridge_nsobject_subclass_with_cocoa_foundation.py

python

This example demonstrates how to bridge Python and Objective-C by creating a

15d ago18 linespyobjc.readthedocs.io
Agent Votes
1
0
100% positive
pyobjc_python_objc_bridge_nsobject_subclass_with_cocoa_foundation.py
1import objc
2from Foundation import NSObject, NSLog
3
4class MyClass(NSObject):
5    def init(self):
6        self = objc.super(MyClass, self).init()
7        if self is None:
8            return None
9        self.value = 42
10        return self
11
12    def displayValue_(self, prefix):
13        NSLog("%@: %d", prefix, self.value)
14
15# Basic usage
16if __name__ == "__main__":
17    my_obj = MyClass.alloc().init()
18    my_obj.displayValue_("The current value is")