Skip to content

Commit 407374a

Browse files
authored
Merge pull request #37 from mlch911/main
fix force unwrapping crash
2 parents a82277a + ac63e77 commit 407374a

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

AssociatedObject.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AssociatedObject"
4-
s.version = "0.8.0"
4+
s.version = "0.10.1"
55
s.summary = "Swift Macro for allowing variable declarations even in class extensions."
66

77
s.description = <<-DESC

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ extension AssociatedObjectMacro {
228228
policy: ExprSyntax,
229229
defaultValue: ExprSyntax?
230230
) -> AccessorDeclSyntax {
231-
let typeWithoutOptional = if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
232-
type.wrappedType
231+
let typeWithoutOptional = if let type = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
232+
type.wrappedType
233+
} else if let type = type.as(OptionalTypeSyntax.self) {
234+
type.wrappedType
233235
} else {
234236
type
235237
}
@@ -253,7 +255,7 @@ extension AssociatedObjectMacro {
253255
return getAssociatedObject(
254256
self,
255257
\(associatedKey)
256-
) as! \(typeWithoutOptional.trimmed)
258+
) as? \(typeWithoutOptional.trimmed)
257259
}
258260
"""
259261
} else if let defaultValue {

Tests/AssociatedObjectTests/AssociatedObjectTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ final class AssociatedObjectTests: XCTestCase {
246246
getAssociatedObject(
247247
self,
248248
Self.__associated_stringKey
249-
) as? String?
249+
) as? String
250250
?? nil
251251
}
252252
set {
@@ -356,7 +356,7 @@ final class AssociatedObjectTests: XCTestCase {
356356
return getAssociatedObject(
357357
self,
358358
Self.__associated_stringKey
359-
) as! String?
359+
) as? String
360360
}
361361
}
362362
set {
@@ -481,7 +481,7 @@ final class AssociatedObjectTests: XCTestCase {
481481
getAssociatedObject(
482482
self,
483483
Self.__associated_boolKey
484-
) as? Bool?
484+
) as? Bool
485485
?? nil
486486
}
487487
set {

0 commit comments

Comments
 (0)