0
# Method and Class Decorators
1
2
Decorators for controlling how Python methods are exposed to the Objective-C runtime, including method signatures, selector customization, and method classification.
3
4
## Capabilities
5
6
### Method Classification Decorators
7
8
Decorators that control whether methods are exposed to the Objective-C runtime or kept as Python-only methods.
9
10
```python { .api }
11
def objc_method(value=None, *, selector=None, signature=None, isclass=None):
12
"""
13
Decorator to mark methods as Objective-C selectors.
14
15
Args:
16
value: Method to decorate (when used without parentheses)
17
selector (str, optional): Override Objective-C selector name
18
signature (bytes, optional): Method signature in Objective-C encoding
19
isclass (bool, optional): True for class methods, False for instance methods
20
21
Returns:
22
Decorated method exposed to Objective-C runtime
23
"""
24
25
def python_method(value=None):
26
"""
27
Decorator to mark methods as Python-only (not Objective-C selectors).
28
29
Args:
30
value: Method to decorate
31
32
Returns:
33
Method that won't be exposed to Objective-C runtime
34
"""
35
```
36
37
### Selector Customization
38
39
Decorators for customizing Objective-C selector names and method signatures.
40
41
```python { .api }
42
def namedSelector(name, signature=None):
43
"""
44
Override the Objective-C selector name for a method.
45
46
Args:
47
name (str): Objective-C selector name
48
signature (bytes, optional): Method signature in Objective-C encoding
49
50
Returns:
51
Decorator function
52
"""
53
54
def namedselector(name, signature=None):
55
"""
56
Alias for namedSelector.
57
58
Args:
59
name (str): Objective-C selector name
60
signature (bytes, optional): Method signature in Objective-C encoding
61
62
Returns:
63
Decorator function
64
"""
65
66
def typedSelector(signature):
67
"""
68
Decorator for specifying typed selectors with explicit signatures.
69
70
Args:
71
signature (bytes): Method signature in Objective-C encoding
72
73
Returns:
74
Decorator function
75
"""
76
```
77
78
### Method Signature Control
79
80
Functions for setting and managing method signatures in the Objective-C runtime.
81
82
```python { .api }
83
def signature(sig):
84
"""
85
Set method signature for Objective-C method.
86
87
Args:
88
sig (bytes): Method signature in Objective-C encoding
89
90
Returns:
91
Decorator function
92
"""
93
94
def instancemethod(func):
95
"""
96
Mark function as an instance method.
97
98
Args:
99
func: Function to mark as instance method
100
101
Returns:
102
Decorated function
103
"""
104
```
105
106
**Usage Examples:**
107
108
```python
109
import objc
110
111
class MyClass:
112
@objc.objc_method
113
def regularMethod(self):
114
"""This method will be available to Objective-C."""
115
return "Hello from Objective-C"
116
117
@objc.python_method
118
def pythonOnlyMethod(self):
119
"""This method is Python-only."""
120
return "Hello from Python"
121
122
@objc.namedSelector("customSelectorName:")
123
def renamedMethod_(self, value):
124
"""Method with custom Objective-C selector name."""
125
return f"Value: {value}"
126
127
@objc.objc_method(signature=b"i@:i")
128
def integerMethod_(self, value):
129
"""Method with explicit signature (returns int, takes int)."""
130
return value * 2
131
132
@objc.objc_method(isclass=True)
133
def classMethod(cls):
134
"""Class method exposed to Objective-C."""
135
return "Class method result"
136
```