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

properties-accessors.mddocs/

Properties and Accessors

Comprehensive property descriptor system for creating Key-Value Coding compliant accessors, synthesized properties, and callback handling for seamless Objective-C property integration.

Capabilities

Key-Value Coding Accessors

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

Property Synthesis

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

Callback and Selector Support

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

Accessor Base Class

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

Integration with Objective-C Properties

These descriptors enable full integration with Objective-C property systems:

  • Automatic KVC compliance: Properties work with Key-Value Coding patterns
  • Type safety: Typed accessors ensure proper type conversion
  • Memory management: Copy semantics and proper retain/release handling
  • Callback support: Proper function pointer handling for callbacks

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