Skip to content

Instantly share code, notes, and snippets.

@hamishknight
Created June 29, 2019 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamishknight/0e7c214fc2d9d05f955a926280c122c2 to your computer and use it in GitHub Desktop.
Save hamishknight/0e7c214fc2d9d05f955a926280c122c2 to your computer and use it in GitHub Desktop.
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