0
# Properties and Accessors
1
2
Comprehensive property descriptor system for creating Key-Value Coding compliant accessors, synthesized properties, and callback handling for seamless Objective-C property integration.
3
4
## Capabilities
5
6
### Key-Value Coding Accessors
7
8
Decorators for creating accessors that comply with Key-Value Coding conventions, enabling automatic property access patterns.
9
10
```python { .api }
11
def accessor(func, typeSignature=b"@"):
12
"""
13
Create a Key-Value Coding compliant accessor method.
14
15
Args:
16
func: Function to convert to accessor
17
typeSignature (bytes): Objective-C type signature (default: object type)
18
19
Returns:
20
Accessor descriptor compatible with KVC
21
"""
22
23
def typedAccessor(typeSignature):
24
"""
25
Decorator for creating typed accessors with specific type signatures.
26
27
Args:
28
typeSignature (bytes): Objective-C type signature
29
30
Returns:
31
Decorator function for typed accessor creation
32
"""
33
```
34
35
### Property Synthesis
36
37
Functions for automatically generating property getter and setter methods.
38
39
```python { .api }
40
def synthesize(name, copy=False, readwrite=True, type=_C_ID, ivarName=None):
41
"""
42
Generate property accessors automatically.
43
44
Args:
45
name (str): Property name
46
copy (bool): Whether to copy values on assignment (default: False)
47
readwrite (bool): Whether property is read-write (default: True)
48
type: Objective-C type encoding (default: object type)
49
ivarName (str, optional): Instance variable name (defaults to _name)
50
51
Returns:
52
Property descriptor with generated accessors
53
"""
54
```
55
56
### Callback and Selector Support
57
58
Functions for handling callbacks and ensuring proper selector signatures.
59
60
```python { .api }
61
def callbackFor(callable, argIndex=-1):
62
"""
63
Decorator for callback functions, ensuring proper signature handling.
64
65
Args:
66
callable: Function to use as callback
67
argIndex (int): Argument index for callback (default: -1 for last)
68
69
Returns:
70
Properly configured callback descriptor
71
"""
72
73
def selectorFor(callable, argIndex=-1):
74
"""
75
Ensure correct signature for selectors passed as arguments.
76
77
Args:
78
callable: Function to use as selector
79
argIndex (int): Argument index for selector (default: -1 for last)
80
81
Returns:
82
Selector with correct signature
83
"""
84
85
def callbackPointer(closure):
86
"""
87
Get function pointer for callback closure.
88
89
Args:
90
closure: Closure function to convert to pointer
91
92
Returns:
93
Function pointer suitable for Objective-C callbacks
94
"""
95
```
96
97
### Accessor Base Class
98
99
Base class for creating custom property descriptors.
100
101
```python { .api }
102
class Accessor:
103
"""
104
Base class for accessor descriptors providing property-like access.
105
106
Enables creation of custom property descriptors that integrate
107
with the Objective-C property system.
108
"""
109
```
110
111
**Usage Examples:**
112
113
```python
114
import objc
115
from objc import accessor, typedAccessor, synthesize, callbackFor
116
117
class MyClass:
118
# Synthesized property with automatic getter/setter
119
title = synthesize("title", copy=True, readwrite=True)
120
121
# Read-only synthesized property
122
identifier = synthesize("identifier", readwrite=False)
123
124
# Custom accessor with type signature
125
@accessor
126
def customValue(self):
127
return self._customValue
128
129
@customValue.setter
130
@typedAccessor(b"i") # Integer type
131
def customValue(self, value):
132
self._customValue = int(value)
133
134
# Method with callback parameter
135
@callbackFor(lambda self, result: self.handleResult(result))
136
def performAsyncOperation_(self, callback):
137
# Perform operation, then call callback
138
result = self.doWork()
139
callback(result)
140
141
def handleResult(self, result):
142
print(f"Operation completed with result: {result}")
143
144
# Using KVC-compliant accessors
145
obj = MyClass()
146
obj.title = "Hello World" # Uses synthesized setter
147
print(obj.title) # Uses synthesized getter
148
149
# Properties work with Key-Value Coding
150
from PyObjCTools.KeyValueCoding import setKey, getKey
151
setKey(obj, "title", "New Title")
152
current_title = getKey(obj, "title")
153
```
154
155
### Integration with Objective-C Properties
156
157
These descriptors enable full integration with Objective-C property systems:
158
159
- **Automatic KVC compliance**: Properties work with Key-Value Coding patterns
160
- **Type safety**: Typed accessors ensure proper type conversion
161
- **Memory management**: Copy semantics and proper retain/release handling
162
- **Callback support**: Proper function pointer handling for callbacks