-
-
Save hamishknight/0e7c214fc2d9d05f955a926280c122c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol StoryItem { | |
var id: Int64? { get } | |
} | |
public protocol TypeErasedRestorable : AnyObject { | |
var typeErasedStoryItem: StoryItem? { get } | |
} | |
public protocol Restorable : TypeErasedRestorable { | |
associatedtype T : StoryItem | |
var storyItem: T? { get set } | |
} | |
extension Restorable { | |
var typeErasedStoryItem: StoryItem? { storyItem } | |
} | |
public struct LabelItem: StoryItem { | |
public var id: Int64? | |
public var text: String? | |
} | |
public struct StickerItem: StoryItem { | |
public var id: Int64? | |
public var imageName: String? | |
} | |
class LabelView: UILabel, Restorable { | |
var storyItem: LabelItem? | |
} | |
class StickerView: UIImageView, Restorable { | |
var storyItem: StickerItem? | |
} | |
class SomeView: UIView { | |
func foo() { | |
let restorableSubviews = subviews.compactMap({$0 as? (TypeErasedRestorable & UIView)}) | |
print(restorableSubviews.map { $0.typeErasedStoryItem }) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment