Skip to content

Commit 4b0758d

Browse files
authored
Merge pull request #46 from mlch911/fix/DefaultValue
fix optional type set before get
2 parents 2f6b531 + 4219b78 commit 4b0758d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+8-1
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 {
@@ -336,6 +338,11 @@ extension AssociatedObjectMacro {
336338
\(policy)
337339
)
338340
"""
341+
if type.isOptional, hasDefaultValue {
342+
"""
343+
self.__associated_\(identifier.trimmed)IsSet = true
344+
"""
345+
}
339346

340347
if let didSet = `didSet`,
341348
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)