Python-to-Objective-C bridge providing seamless interoperability between Python and Objective-C programming languages on macOS systems.
—
Support for Objective-C formal and informal protocols, enabling proper protocol conformance, method requirements, and protocol-based programming patterns.
Functions for working with Objective-C formal protocols, equivalent to @protocol() declarations.
def protocolNamed(name):
"""
Get a formal protocol by name (equivalent to @protocol(name) in Objective-C).
Args:
name (str): Name of the protocol to retrieve
Returns:
Protocol object that can be used for conformance checking
Raises:
ProtocolError: If protocol cannot be found or loaded
"""Functions for working with Objective-C informal protocols (categories on NSObject).
def informal_protocol(name):
"""
Create or access an informal protocol.
Args:
name (str): Name of the informal protocol
Returns:
Informal protocol object for method declarations
"""Exception class for protocol-related errors.
class ProtocolError(Exception):
"""
Exception raised for protocol-related errors such as:
- Protocol not found
- Protocol loading failures
- Protocol conformance issues
"""Usage Examples:
import objc
from objc import protocolNamed, informal_protocol, ProtocolError
# Working with formal protocols
try:
# Get a system protocol
delegate_protocol = protocolNamed("NSApplicationDelegate")
table_delegate = protocolNamed("NSTableViewDelegate")
# Check if class conforms to protocol
# (typically done through runtime introspection)
except ProtocolError as e:
print(f"Protocol error: {e}")
# Implementing protocol methods in Python class
class MyAppDelegate:
"""Application delegate implementing NSApplicationDelegate protocol."""
@objc.objc_method
def applicationDidFinishLaunching_(self, notification):
"""Required NSApplicationDelegate method."""
print("Application finished launching")
@objc.objc_method
def applicationShouldTerminate_(self, sender):
"""Optional NSApplicationDelegate method."""
return True # Allow termination
class MyTableDelegate:
"""Table view delegate implementing NSTableViewDelegate protocol."""
@objc.objc_method
def tableView_heightOfRow_(self, tableView, row):
"""NSTableViewDelegate method for row height."""
return 25.0
@objc.objc_method
def tableView_shouldSelectRow_(self, tableView, row):
"""NSTableViewDelegate method for selection control."""
return True
# Working with informal protocols
# Informal protocols are typically used for optional method collections
my_informal = informal_protocol("MyCustomProtocol")PyObjC enables protocol conformance through method implementation:
@objc_method decorator with appropriate signatures# Delegate pattern implementation
class MyController:
def __init__(self):
self.tableView = None
def setupTableView(self):
# Set self as delegate (must conform to protocol)
self.tableView.setDelegate_(self)
# Implement required delegate methods
@objc.objc_method
def numberOfRowsInTableView_(self, tableView):
return 10
@objc.objc_method
def tableView_objectValueForTableColumn_row_(self, tableView, column, row):
return f"Row {row}"
# Data source pattern implementation
class MyDataSource:
@objc.objc_method
def tableView_numberOfRowsInSection_(self, tableView, section):
return len(self.data)
@objc.objc_method
def tableView_cellForRowAtIndexPath_(self, tableView, indexPath):
# Return configured cell
passInstall with Tessl CLI
npx tessl i tessl/pypi-pyobjc-core