Skip to content

Commit 635675b

Browse files
committed
fix optional type set before get
# Conflicts: # Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift
1 parent 2f6b531 commit 635675b

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+23-9
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ extension AssociatedObjectMacro: AccessorMacro {
208208
identifier: identifier,
209209
type: type,
210210
policy: policy,
211-
associatedKey: associatedKey,
211+
associatedKey: associatedKey,
212+
hasDefaultValue: defaultValue != nil,
212213
willSet: binding.willSet,
213214
didSet: binding.didSet
214215
)
@@ -306,6 +307,7 @@ extension AssociatedObjectMacro {
306307
type: TypeSyntax,
307308
policy: ExprSyntax,
308309
associatedKey: ExprSyntax,
310+
hasDefaultValue: Bool,
309311
`willSet`: AccessorDeclSyntax?,
310312
`didSet`: AccessorDeclSyntax?
311313
) -> AccessorDeclSyntax {
@@ -328,14 +330,26 @@ extension AssociatedObjectMacro {
328330
"let oldValue = \(identifier)"
329331
}
330332

331-
"""
332-
setAssociatedObject(
333-
self,
334-
\(associatedKey),
335-
newValue,
336-
\(policy)
337-
)
338-
"""
333+
if type.isOptional, hasDefaultValue {
334+
"""
335+
setAssociatedObject(
336+
self,
337+
\(associatedKey),
338+
newValue,
339+
\(policy)
340+
)
341+
self.__associated_\(identifier.trimmed)IsSet = true
342+
"""
343+
} else {
344+
"""
345+
setAssociatedObject(
346+
self,
347+
\(associatedKey),
348+
newValue,
349+
\(policy)
350+
)
351+
"""
352+
}
339353

340354
if let didSet = `didSet`,
341355
let body = didSet.body {

Tests/AssociatedObjectTests/AssociatedObjectTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ final class AssociatedObjectTests: XCTestCase {
385385
newValue,
386386
.retain(.nonatomic)
387387
)
388+
self.__associated_stringIsSet = true
388389
}
389390
}
390391

0 commit comments

Comments
 (0)