Python-to-Objective-C bridge providing seamless interoperability between Python and Objective-C programming languages on macOS systems.
—
Comprehensive property descriptor system for creating Key-Value Coding compliant accessors, synthesized properties, and callback handling for seamless Objective-C property integration.
Decorators for creating accessors that comply with Key-Value Coding conventions, enabling automatic property access patterns.
def accessor(func, typeSignature=b"@"):
"""
Create a Key-Value Coding compliant accessor method.
Args:
func: Function to convert to accessor
typeSignature (bytes): Objective-C type signature (default: object type)
Returns:
Accessor descriptor compatible with KVC
"""
def typedAccessor(typeSignature):
"""
Decorator for creating typed accessors with specific type signatures.
Args:
typeSignature (bytes): Objective-C type signature
Returns:
Decorator function for typed accessor creation
"""Functions for automatically generating property getter and setter methods.
def synthesize(name, copy=False, readwrite=True, type=_C_ID, ivarName=None):
"""
Generate property accessors automatically.
Args:
name (str): Property name
copy (bool): Whether to copy values on assignment (default: False)
readwrite (bool): Whether property is read-write (default: True)
type: Objective-C type encoding (default: object type)
ivarName (str, optional): Instance variable name (defaults to _name)
Returns:
Property descriptor with generated accessors
"""Functions for handling callbacks and ensuring proper selector signatures.
def callbackFor(callable, argIndex=-1):
"""
Decorator for callback functions, ensuring proper signature handling.
Args:
callable: Function to use as callback
argIndex (int): Argument index for callback (default: -1 for last)
Returns:
Properly configured callback descriptor
"""
def selectorFor(callable, argIndex=-1):
"""
Ensure correct signature for selectors passed as arguments.
Args:
callable: Function to use as selector
argIndex (int): Argument index for selector (default: -1 for last)
Returns:
Selector with correct signature
"""
def callbackPointer(closure):
"""
Get function pointer for callback closure.
Args:
closure: Closure function to convert to pointer
Returns:
Function pointer suitable for Objective-C callbacks
"""Base class for creating custom property descriptors.
class Accessor:
"""
Base class for accessor descriptors providing property-like access.
Enables creation of custom property descriptors that integrate
with the Objective-C property system.
"""Usage Examples:
import objc
from objc import accessor, typedAccessor, synthesize, callbackFor
class MyClass:
# Synthesized property with automatic getter/setter
title = synthesize("title", copy=True, readwrite=True)
# Read-only synthesized property
identifier = synthesize("identifier", readwrite=False)
# Custom accessor with type signature
@accessor
def customValue(self):
return self._customValue
@customValue.setter
@typedAccessor(b"i") # Integer type
def customValue(self, value):
self._customValue = int(value)
# Method with callback parameter
@callbackFor(lambda self, result: self.handleResult(result))
def performAsyncOperation_(self, callback):
# Perform operation, then call callback
result = self.doWork()
callback(result)
def handleResult(self, result):
print(f"Operation completed with result: {result}")
# Using KVC-compliant accessors
obj = MyClass()
obj.title = "Hello World" # Uses synthesized setter
print(obj.title) # Uses synthesized getter
# Properties work with Key-Value Coding
from PyObjCTools.KeyValueCoding import setKey, getKey
setKey(obj, "title", "New Title")
current_title = getKey(obj, "title")These descriptors enable full integration with Objective-C property systems:
Install with Tessl CLI
npx tessl i tessl/pypi-pyobjc-core