@@ -78,7 +78,7 @@ extension AssociatedObjectMacro: PeerMacro {
78
78
let flagName = " __associated_ \( identifier. trimmed) IsSet "
79
79
let flagDecl = VariableDeclSyntax (
80
80
attributes: [
81
- . attribute( " @_AssociatedObject(.OBJC_ASSOCIATION_ASSIGN ) " )
81
+ . attribute( " @_AssociatedObject(.retain(.nonatomic) ) " )
82
82
] ,
83
83
bindingSpecifier: . identifier( " var " ) ,
84
84
bindings: PatternBindingListSyntax {
@@ -176,15 +176,18 @@ extension AssociatedObjectMacro: AccessorMacro {
176
176
return [ ]
177
177
}
178
178
179
- guard case let . argumentList( arguments) = node. arguments,
180
- let firstElement = arguments. first? . expression,
181
- let policy = firstElement. as ( ExprSyntax . self) else {
179
+ guard case let . argumentList( arguments) = node. arguments else {
182
180
return [ ]
183
181
}
184
182
183
+ var policy : ExprSyntax = " .retain(.nonatomic) "
184
+ if let firstElement = arguments. first? . expression,
185
+ let specifiedPolicy = firstElement. as ( ExprSyntax . self) {
186
+ policy = specifiedPolicy
187
+ }
188
+
185
189
var associatedKey : ExprSyntax = " Self.__associated_ \( identifier. trimmed) Key "
186
- if case let . argumentList( arguments) = node. arguments,
187
- let element = arguments. first ( where: { $0. label? . text == " key " } ) ,
190
+ if let element = arguments. first ( where: { $0. label? . text == " key " } ) ,
188
191
let customKey = element. expression. as ( ExprSyntax . self) {
189
192
// Provide store key from outside the macro
190
193
associatedKey = " & \( customKey) "
@@ -225,7 +228,7 @@ extension AssociatedObjectMacro {
225
228
policy: ExprSyntax ,
226
229
defaultValue: ExprSyntax ?
227
230
) -> AccessorDeclSyntax {
228
- let varTypeWithoutOptional = if let type = type. as ( ImplicitlyUnwrappedOptionalTypeSyntax . self) {
231
+ let typeWithoutOptional = if let type = type. as ( ImplicitlyUnwrappedOptionalTypeSyntax . self) {
229
232
type. wrappedType
230
233
} else {
231
234
type
@@ -234,51 +237,49 @@ extension AssociatedObjectMacro {
234
237
return AccessorDeclSyntax (
235
238
accessorSpecifier: . keyword( . get) ,
236
239
body: CodeBlockSyntax {
237
- if let defaultValue {
238
- if type. isOptional {
239
- """
240
- if !self.__associated_ \( identifier. trimmed) IsSet {
241
- let value: \( type. trimmed) = \( defaultValue. trimmed)
242
- objc_setAssociatedObject(
243
- self,
244
- \( associatedKey) ,
245
- value,
246
- \( policy. trimmed)
247
- )
248
- self.__associated_ \( identifier. trimmed) IsSet = true
249
- return value
250
- } else {
251
- return objc_getAssociatedObject(
252
- self,
253
- \( associatedKey)
254
- ) as! \( varTypeWithoutOptional. trimmed)
255
- }
256
- """
240
+ if let defaultValue, type. isOptional {
241
+ """
242
+ if !self.__associated_ \( identifier. trimmed) IsSet {
243
+ let value: \( type. trimmed) = \( defaultValue. trimmed)
244
+ setAssociatedObject(
245
+ self,
246
+ \( associatedKey) ,
247
+ value,
248
+ \( policy. trimmed)
249
+ )
250
+ self.__associated_ \( identifier. trimmed) IsSet = true
251
+ return value
257
252
} else {
258
- """
259
- if let value = objc_getAssociatedObject(
253
+ return getAssociatedObject(
260
254
self,
261
255
\( associatedKey)
262
- ) as? \( varTypeWithoutOptional. trimmed) {
263
- return value
264
- } else {
265
- let value: \( type. trimmed) = \( defaultValue. trimmed)
266
- objc_setAssociatedObject(
267
- self,
268
- \( associatedKey) ,
269
- value,
270
- \( policy. trimmed)
271
- )
272
- return value
273
- }
274
- """
256
+ ) as! \( typeWithoutOptional. trimmed)
275
257
}
258
+ """
259
+ } else if let defaultValue {
260
+ """
261
+ if let value = getAssociatedObject(
262
+ self,
263
+ \( associatedKey)
264
+ ) as? \( typeWithoutOptional. trimmed) {
265
+ return value
266
+ } else {
267
+ let value: \( type. trimmed) = \( defaultValue. trimmed)
268
+ setAssociatedObject(
269
+ self,
270
+ \( associatedKey) ,
271
+ value,
272
+ \( policy. trimmed)
273
+ )
274
+ return value
275
+ }
276
+ """
276
277
} else {
277
278
"""
278
- objc_getAssociatedObject (
279
+ getAssociatedObject (
279
280
self,
280
281
\( associatedKey)
281
- ) as? \( varTypeWithoutOptional . trimmed)
282
+ ) as? \( typeWithoutOptional . trimmed)
282
283
?? \( defaultValue ?? " nil " )
283
284
"""
284
285
}
@@ -324,7 +325,7 @@ extension AssociatedObjectMacro {
324
325
}
325
326
326
327
"""
327
- objc_setAssociatedObject (
328
+ setAssociatedObject (
328
329
self,
329
330
\( associatedKey) ,
330
331
newValue,
0 commit comments