PyObjC is a bridge between Python and Objective-C that allows full featured Cocoa applications to be written in pure Python.
—
Type encoding, signature parsing, struct type creation, and bridge support utilities. The type system handles the complex conversion between Python and Objective-C type systems, enabling seamless interoperability between the two languages.
Functions for parsing and manipulating Objective-C type signatures.
def splitSignature(signature: str):
"""
Parse an Objective-C method type signature into components.
Args:
signature (str): Objective-C type encoding string (e.g., "v@:i")
Returns:
tuple: (return_type, arg_types) where each is a type encoding
Usage:
return_type, arg_types = objc.splitSignature("i@:@@")
# return_type = "i" (int)
# arg_types = ["@", ":", "@", "@"] (object, selector, object, object)
"""
def splitStructSignature(signature: str):
"""
Parse a struct type signature into field components.
Args:
signature (str): Struct type encoding string
Returns:
list: List of field type encodings
"""Functions for creating and managing custom struct types that bridge between Python and Objective-C.
def createStructType(name: str, signature: str, typeids):
"""
Create a new struct type for use in the bridge.
Args:
name (str): Name for the struct type
signature (str): Type encoding signature for the struct
typeids: Type identifier information
Returns:
A new struct type class that can be used in Python
Usage:
PointStruct = objc.createStructType(
"Point",
"{CGPoint=dd}", # struct with two doubles
None
)
point = PointStruct(10.0, 20.0)
"""
def createOpaquePointerType(name: str, doc: str = None):
"""
Create an opaque pointer type for the bridge.
Args:
name (str): Name of the opaque pointer type
doc (str, optional): Documentation string
Returns:
The new opaque pointer type class
Usage:
FileHandle = objc.createOpaquePointerType("FileHandle", "File handle pointer")
"""
def registerStructAlias(originalName: str, aliasName: str):
"""
Register an alias for an existing struct type.
Args:
originalName (str): Original struct type name
aliasName (str): Alias name to register
Usage:
objc.registerStructAlias("CGPoint", "NSPoint")
"""Functions for registering metadata about Objective-C methods to improve bridge behavior.
def registerMetaDataForSelector(classname: str, selector: str, metadata):
"""
Register metadata for a specific method selector.
Args:
classname (str): Name of the Objective-C class
selector (str): Method selector name
metadata: Dictionary containing method metadata
Metadata can include:
- Method signature
- Argument type information
- Return type information
- Memory management hints
"""Low-level functions for calling Objective-C methods with proper type conversion.
def callInstanceMethod(obj, selector: str, signature: str, args: tuple, kwds: dict):
"""
Call an instance method on an Objective-C object.
Args:
obj: The Objective-C object instance
selector (str): Method selector name
signature (str): Method type signature
args (tuple): Positional arguments
kwds (dict): Keyword arguments
Returns:
The method's return value, converted to Python types
"""
def callClassMethod(cls, selector: str, signature: str, args: tuple, kwds: dict):
"""
Call a class method on an Objective-C class.
Args:
cls: The Objective-C class
selector (str): Method selector name
signature (str): Method type signature
args (tuple): Positional arguments
kwds (dict): Keyword arguments
Returns:
The method's return value, converted to Python types
"""Functions for integrating with Python's ABC (Abstract Base Class) system.
def registerABCForClass(cls):
"""
Register an Abstract Base Class for an Objective-C class.
Args:
cls: The Objective-C class to register ABC support for
This enables isinstance() and issubclass() checks between
Python ABCs and Objective-C classes.
"""PyObjC uses Objective-C type encoding characters to represent types:
# Basic type encodings
_C_ID = b'@' # Objective-C object
_C_CLASS = b'#' # Class object
_C_SEL = b':' # Selector
_C_CHR = b'c' # Character (signed char)
_C_UCHR = b'C' # Unsigned character
_C_SHT = b's' # Short
_C_USHT = b'S' # Unsigned short
_C_INT = b'i' # Integer
_C_UINT = b'I' # Unsigned integer
_C_LNG = b'l' # Long
_C_ULNG = b'L' # Unsigned long
_C_FLT = b'f' # Float
_C_DBL = b'd' # Double
_C_BOOL = b'B' # Boolean
_C_VOID = b'v' # Void
_C_PTR = b'^' # Pointer
_C_STRUCT_B = b'{' # Structure begin
_C_STRUCT_E = b'}' # Structure end
_C_ARY_B = b'[' # Array begin
_C_ARY_E = b']' # Array endHigh-performance vector and matrix types for mathematical operations and graphics programming.
class simd_int2:
"""2-component 32-bit signed integer vector"""
class simd_int3:
"""3-component 32-bit signed integer vector"""
class simd_int4:
"""4-component 32-bit signed integer vector"""
class simd_uint2:
"""2-component 32-bit unsigned integer vector"""
class simd_uint3:
"""3-component 32-bit unsigned integer vector"""
class simd_uint4:
"""4-component 32-bit unsigned integer vector"""class simd_float2:
"""2-component 32-bit float vector"""
class simd_float3:
"""3-component 32-bit float vector"""
class simd_float4:
"""4-component 32-bit float vector"""
class simd_double2:
"""2-component 64-bit double vector"""
class simd_double3:
"""3-component 64-bit double vector"""
class simd_double4:
"""4-component 64-bit double vector"""class simd_short2:
"""2-component 16-bit signed integer vector"""
class simd_ushort2:
"""2-component 16-bit unsigned integer vector"""
class simd_ushort3:
"""3-component 16-bit unsigned integer vector"""
class simd_ushort4:
"""4-component 16-bit unsigned integer vector"""
class simd_uchar16:
"""16-component 8-bit unsigned character vector"""class matrix_float2x2:
"""2x2 matrix of 32-bit floats"""
class matrix_float3x3:
"""3x3 matrix of 32-bit floats"""
class matrix_float4x3:
"""4x3 matrix of 32-bit floats"""
class matrix_float4x4:
"""4x4 matrix of 32-bit floats"""
class matrix_double4x4:
"""4x4 matrix of 64-bit doubles"""class simd_quatf:
"""Quaternion with 32-bit float components"""
class simd_quatd:
"""Quaternion with 64-bit double components"""# Legacy aliases for backward compatibility
vector_int2 = simd_int2
vector_int3 = simd_int3
vector_int4 = simd_int4
vector_uint2 = simd_uint2
vector_uint3 = simd_uint3
vector_uint4 = simd_uint4
vector_float2 = simd_float2
vector_float3 = simd_float3
vector_float4 = simd_float4
vector_double2 = simd_double2
vector_double3 = simd_double3
vector_double4 = simd_double4
vector_short2 = simd_short2
vector_ushort2 = simd_ushort2
vector_ushort3 = simd_ushort3
vector_ushort4 = simd_ushort4
vector_uchar16 = simd_uchar16import objc
# Parse a method signature
signature = "i@:@@" # int methodWithObject:andObject:
return_type, arg_types = objc.splitSignature(signature)
print(f"Return type: {return_type}") # "i" (integer)
print(f"Argument types: {arg_types}") # ["@", ":", "@", "@"]
# The arguments are:
# "@" - self (object)
# ":" - _cmd (selector)
# "@" - first object parameter
# "@" - second object parameterimport objc
# Create a Point struct type
Point = objc.createStructType(
"Point",
"{Point=dd}", # struct with two doubles: x and y
None
)
# Use the struct type
point1 = Point(10.0, 20.0)
point2 = Point(x=30.0, y=40.0)
print(f"Point 1: ({point1.x}, {point1.y})") # (10.0, 20.0)
print(f"Point 2: ({point2.x}, {point2.y})") # (30.0, 40.0)
# Register an alias for the struct
objc.registerStructAlias("CGPoint", Point)import objc
# Register metadata for a custom method
objc.registerMetaDataForSelector(
"MyCustomClass",
"processData:withOptions:",
{
'signature': b'@@:@@',
'arguments': {
2: {'type': b'@', 'description': 'Input data'},
3: {'type': b'@', 'description': 'Processing options'}
},
'return': {'type': b'@', 'description': 'Processed result'}
}
)import objc
from Foundation import NSString
# Get a string object
string_obj = NSString.stringWithString_("Hello")
# Call a method using the low-level interface
result = objc.callInstanceMethod(
string_obj,
"uppercaseString",
"@@:", # return object, self object, selector
(), # no additional arguments
{} # no keyword arguments
)
print(result) # "HELLO"Install with Tessl CLI
npx tessl i tessl/pypi-pyobjc