CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyobjc-core

Python-to-Objective-C bridge providing seamless interoperability between Python and Objective-C programming languages on macOS systems.

Pending
Overview
Eval results
Files

protocol-support.mddocs/

Protocol Support

Support for Objective-C formal and informal protocols, enabling proper protocol conformance, method requirements, and protocol-based programming patterns.

Capabilities

Formal Protocol Support

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
    """

Informal Protocol Support

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
    """

Protocol Error Handling

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")

Protocol Conformance Patterns

PyObjC enables protocol conformance through method implementation:

  1. Formal Protocols: Implement required and optional methods as defined by the protocol
  2. Method Signatures: Use @objc_method decorator with appropriate signatures
  3. Runtime Registration: Methods are automatically registered with the Objective-C runtime
  4. Type Safety: Protocol conformance is checked at runtime by the Objective-C system

Common Protocol Patterns

# 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
        pass

Install with Tessl CLI

npx tessl i tessl/pypi-pyobjc-core

docs

bridge-registration.md

class-enhancement.md

core-bridge.md

framework-loading.md

index.md

interface-builder.md

method-decorators.md

properties-accessors.md

protocol-support.md

pyobjctools.md

tile.json