Last active
July 22, 2016 18:15
-
-
Save b3ll/7fa7f5515cb9170b296fd6e75726678c to your computer and use it in GitHub Desktop.
Sadface
This file contains hidden or 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
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| protocol Item { | |
| var title: String? { get set } | |
| var value: String? { get set } | |
| } | |
| protocol ItemContainerViewDelegate { | |
| func itemContainerView<T: Item>(itemContainerView: ItemContainerView<T>, didSelectItem item: T?) | |
| } | |
| class ItemContainerView<T: Item>: UIView { | |
| var delegate: ItemContainerViewDelegate? | |
| func onTap() { | |
| // error: cannot convert value of type ItemContainerView<T> to expected argument type ItemContainerView<_> | |
| delegate?.itemContainerView(self, didSelectItem: nil) | |
| } | |
| } |
Author
// We need to declare ItemContainerView here as ItemContainerView<ItemType, Delegate> but that'd be referencing itself
@b3ll Oh yeah, I forgot about that. You could do type erasure, but I don't know if it's worth it. Do you really need to pass the entire ItemContainerView?
Author
@NachoSoto well the cocoa way to do delegates is to pass back the original object that's firing the delegate, so without that it's not as useful. i.e. tableView:deselectRowAtIndexPath:animated:. I suppose I could pass back the index of the object, but that wouldn't be as nice than the object itself.
I'm not sure if type erasure is possible at this point, anything other than this I've tried has just lead to the compiler complaining about associatedtype dependencies :/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually it looks like @NachoSoto's suggestion won't work :(