Last active
August 3, 2019 23:27
-
-
Save PaulWoodIII/a81dc7290e5b791c3c8571ec5b8b7c48 to your computer and use it in GitHub Desktop.
Protocol Associated Types with SwiftUI Identifiable will not work
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
| //: [Previous](@previous) | |
| import Foundation | |
| import SwiftUI | |
| import UIKit | |
| import PlaygroundSupport | |
| // This doesnt work because the protocol has an assoicated Type | |
| // To understand more | |
| // https://chariotsolutions.com/screencast/philly-ete-2019-rob-napier-generic-swift-it-isnt-supposed-to-hurt/ | |
| // | |
| protocol Nameable: Identifiable { | |
| var name: String { get } | |
| } | |
| struct Name: Nameable { | |
| var name: String | |
| public init(_ value: String) { | |
| self.name = value | |
| } | |
| var id: String { return name } | |
| } | |
| struct Surname: Nameable { | |
| var name: String | |
| public init(_ value: String) { | |
| self.name = value | |
| } | |
| var id: String { return name } | |
| } | |
| let names: [Nameable] = [ | |
| Name("Alice"), | |
| Name("Bob"), | |
| Surname("Clark"), | |
| Surname("Davis"), | |
| Name("Elanore"), | |
| Name("Frank"), | |
| Surname("Gonzalez"), | |
| Surname("Harris"), | |
| ] | |
| struct SimpleIdentifiableList : View { | |
| var body: some View { | |
| List(names) { name in | |
| Text(name.name) | |
| } | |
| } | |
| } | |
| let viewController = UIHostingController(rootView: SimpleIdentifiableList()) | |
| PlaygroundPage.current.liveView = viewController | |
| //: [Next](@next) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment