Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit 85d9a51

Browse files
drumnkylestaguer
authored andcommitted
Fixed Swift ObjC Wrapper of layout (#212)
There could be an issue if you inherit from one of the provided Objective C layouts from LayoutKit. This fix makes it so that if you do that, it will call the correct overridden methods.
1 parent 3272448 commit 85d9a51

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

Sources/ObjCSupport/LOKLayout.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ import CoreGraphics
2020

2121
extension LOKLayout {
2222
var unwrapped: Layout {
23-
return (self as? WrappedLayout)?.layout ?? (self as? LOKBaseLayout)?.layout ?? ReverseWrappedLayout(layout: self)
23+
/*
24+
Need to check to see if `self` is one of the LayoutKit provided layouts.
25+
If so, we want to cast it as `LOKBaseLayout` and return the wrapped layout
26+
object directly.
27+
28+
If `self` is not one of the LayoutKit provided layouts, we want to wrap it
29+
so that the methods of the class are called. We do this to make sure that if
30+
someone were to subclass one of the LayoutKit provided layouts, we would want
31+
to call their overriden methods instead of just the underlying layout object directly.
32+
*/
33+
let allLayoutClasses = [
34+
LOKButtonLayout.self,
35+
LOKInsetLayout.self,
36+
LOKLabelLayout.self,
37+
LOKOverlayLayout.self,
38+
LOKSizeLayout.self,
39+
LOKStackLayout.self,
40+
LOKTextViewLayout.self
41+
]
42+
if let object = self as? NSObject {
43+
if allLayoutClasses.contains(where: { object.isMember(of: $0) }) {
44+
// Executes if `self` is one of the LayoutKit provided classes; not if it's a subclass
45+
guard let layout = (self as? LOKBaseLayout)?.layout else {
46+
assertionFailure("LayoutKit provided layout does not inherit from LOKBaseLayout")
47+
return ReverseWrappedLayout(layout: self)
48+
}
49+
return layout
50+
}
51+
}
52+
return (self as? WrappedLayout)?.layout ?? ReverseWrappedLayout(layout: self)
2453
}
2554
}

0 commit comments

Comments
 (0)