diff --git a/AssociatedObject.podspec b/AssociatedObject.podspec index f6e0496..a7ed0be 100644 --- a/AssociatedObject.podspec +++ b/AssociatedObject.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "AssociatedObject" - s.version = "0.8.0" + s.version = "0.10.1" s.summary = "Swift Macro for allowing variable declarations even in class extensions." s.description = <<-DESC diff --git a/Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift b/Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift index b5bda97..71be001 100644 --- a/Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift +++ b/Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift @@ -228,8 +228,10 @@ extension AssociatedObjectMacro { policy: ExprSyntax, defaultValue: ExprSyntax? ) -> AccessorDeclSyntax { - let typeWithoutOptional = if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) { - type.wrappedType + let typeWithoutOptional = if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) { + type.wrappedType + } else if let type = type.as(OptionalTypeSyntax.self) { + type.wrappedType } else { type } @@ -253,7 +255,7 @@ extension AssociatedObjectMacro { return getAssociatedObject( self, \(associatedKey) - ) as! \(typeWithoutOptional.trimmed) + ) as? \(typeWithoutOptional.trimmed) } """ } else if let defaultValue { diff --git a/Tests/AssociatedObjectTests/AssociatedObjectTests.swift b/Tests/AssociatedObjectTests/AssociatedObjectTests.swift index 7a5e687..a220e78 100644 --- a/Tests/AssociatedObjectTests/AssociatedObjectTests.swift +++ b/Tests/AssociatedObjectTests/AssociatedObjectTests.swift @@ -246,7 +246,7 @@ final class AssociatedObjectTests: XCTestCase { getAssociatedObject( self, Self.__associated_stringKey - ) as? String? + ) as? String ?? nil } set { @@ -356,7 +356,7 @@ final class AssociatedObjectTests: XCTestCase { return getAssociatedObject( self, Self.__associated_stringKey - ) as! String? + ) as? String } } set { @@ -481,7 +481,7 @@ final class AssociatedObjectTests: XCTestCase { getAssociatedObject( self, Self.__associated_boolKey - ) as? Bool? + ) as? Bool ?? nil } set {